gpt4 book ai didi

python : How to prompt user to enter numbers to an empty list?

转载 作者:行者123 更新时间:2023-12-01 03:19:26 24 4
gpt4 key购买 nike

所以,我知道如何计算 list: list = [], 但我想要求用户输入随机数;但是,如果用户输入 0,则退出,这意味着

list is : [number1, number2, number3, ..., 0].

我尝试写:list = input("请输入数字:",[]),但好像不对。

最佳答案

您需要使用 while 循环来保持程序继续运行,直到用户输入 0。

# Don't call this 'list', it's a reserved keyword in python
lst = []

user_input = int(input("Please enter a number: "))

while user_input != 0:
lst.append(user_input)

user_input = int(input("Please enter a number: "))

print("You said: " + lst)

或者,如果您不想重复 input 行,则可以使用换行符:

lst = []

while True:
user_input = int(input("Please enter a number: "))

if user_input == 0:
break

lst.append(user_input)

print("You said: " + lst)

关于 python : How to prompt user to enter numbers to an empty list?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42059748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com