【发布时间】:2016-10-11 01:47:09
【问题描述】:
一切正常,除了当用户键入 N 来结束 while 循环时,它不会进入 For 语句(当你运行程序时会发生这种情况,在 shell 和 py 中都可以正常工作)。
potato = []
count = 0
avg = 0
question = input('Finding averages, continue? Y or N: ')
while question == 'Y' and count <= 12:
num = int(input('Enter a number: '))
potato.append(num)
count += 1
question = input('Continue? Y or N: ')
for fries in potato:
avg = sum(potato)/count
print(fries,fries-avg)
print('average is: ' + str(avg))
【问题讨论】:
-
您是否收到错误消息?显示有问题。
-
无限循环?不是
break? -
这对我来说很好,也许这是 python 2 -> 3 的问题?
-
提示:
avg = sum(potato)/kount不需要在 for 循环中 -
在 python 3 中,如果你输入
Y,你会在 python 2 中得到想要的结果,它会尝试评估值并抱怨NameError: name 'Y' is not defined。当然,这是一个很好的例子,说明为什么一个完整的问题以及所有错误输出都很有价值。
标签: python python-3.x input