【发布时间】:2018-07-18 06:25:37
【问题描述】:
我觉得这个问题的措辞不好,但这是我真正想要的:
我正在编写这段代码,其中“用户”可以根据需要输入从 1 到 10 的整数。每次用户输入一个整数后,使用是/否类型的问题来询问他/她是否要输入另一个整数。计算并显示列表中整数的平均值。
'while' 不应该一遍又一遍地运行程序的一部分,直到它被告知不要停止时才停止?
num_list = []
len()
integer_pushed = float(input("Enter as many integers from 1 to 10"))
num_list.append(integer_pushed)
again = input("Enter another integer? [y/n]")
while integer_pushed < 0 or integer_pushed > 10:
print('You must type in an integer between 0 and 10')
integer_pushed = float(input("Enter as many integers from 1 to 10"))
num_list.append(integer_pushed)
again = input("Enter another integer? [y/n]")
while again == "y":
integer_pushed = float(input("Enter as many integers from 1 to 10"))
num_list.append(integer_pushed)
again = input("Enter another integer? [y/n]")
print ("Number list:", num_list)
while again == "y":
integer_pushed = float(input("Enter as many integers from 1 to 10"))
num_list.append(integer_pushed)
again = input("Enter another integer? [y/n]")
print ("Number list:", num_list)
即使用户输入“y”,它也会在第二次之后停止。然后它给了我“号码列表:”。
再一次,你们为我和我的同学们提供了很好的帮助。我正在学习 Python 课程,我们正在学习循环和列表。
【问题讨论】:
-
为什么
while again == 'y':循环重复了两次? -
另外这段代码似乎对我有用
标签: python input while-loop append