【问题标题】:Python: this code goes on, but i dont get it [duplicate]Python:这段代码继续,但我不明白[重复]
【发布时间】:2018-12-28 05:48:09
【问题描述】:

所以这是代码,它会永远持续下去,但我无法追踪原因。在某些时候我应该是 10,然后 10

i = 0
numbers = []
abc = range(0, 10)

def loop(i):
    while i < abc:
        print 'at the top i is %d' % i
        numbers.append(i)

        i = i + 1
        print 'NUmbers now: ', numbers
        print 'at the bottom i is %d' %i    

quest = raw_input('> ')    
if quest == 'i':
    loop(i)

print "the numbers: "

for num in numbers:
    print num    

【问题讨论】:

标签: python python-2.7 while-loop infinite-loop


【解决方案1】:

您正在将整数与列表进行比较,该列表始终返回 true (list 大于 int) 。您可能想使用 while I &lt; abc[-1]

【讨论】:

  • 这将导致while I &lt; 9:,而操作需要while I &lt; 10:。使用列表中的最终值似乎有点混淆。我认为我们甚至不需要 while 循环,而不是 for i in range(0, 10): 会使代码惯用。
猜你喜欢
  • 2015-05-19
  • 2012-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多