将while循环转换为函数?蟒蛇

时间:2012-06-22 17:52:04

标签: function while-loop python-2.7

为了压缩我的代码,我试图将我的一个while循环变成一个函数。我已经尝试了很多次,并且在编译时还没有收到与我离开while循环时相同的结果。

这是while循环:

while True:
    i = find_lowest_i(logs)
    if i == -1:
        break
    print "i=", i
    tpl = logs[i].pop(0)
    print tpl
    out.append(tpl)
    print out

以下是我迄今为止的功能:

def mergesort(list_of_logs):
    i = find_lowest_i(logs)
    out = []
    while True:
        if i == -1:
            break
        print "i=", i
        tpl = logs[i].pop(0)
        print tpl
        out.append(tpl)
        print out
    return out

提前致谢。这个地方是初学者程序员的避风港。

1 个答案:

答案 0 :(得分:0)

看起来你的函数的参数是list_of_logs,你仍然在函数体内使用logs。最简单的修复方法可能是将参数从mergesort重命名为list_of_logslogs。否则,对我来说看起来完全正确。

相关问题