[英]Storing Multiple User inputs while using a loop
My intent here this to query the user for multiple inputs using a loop that stops when the user inputs and integer of zero. 我的目的是在用户输入多个输入时使用一个循环,该循环在用户输入且整数为零时停止。 I need to be able to recall the data in a later line of code. 我需要能够在以后的代码行中重新调用数据。 With that in mind I'm trying to create a list of the users input. 考虑到这一点,我试图创建用户输入的列表。
Python 3 code Python 3代码
i = False
val1 = []
while i == False:
if val1 != 0:
val1 = eval(input("Enter an integer, the value ends if it is 0: "))
else:
i = True
print(val1)
I think it would be cleaner if you use a infinite loop and break if the input is 0. Otherwise, simply append to the the list. 我认为如果使用无限循环并在输入为0时中断,那会更干净。否则,只需追加到列表中即可。
values = []
while True:
val = int(input("Enter an integer, the value ends if it is 0: "))
if val == 0:
break
values.append(val)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.