根据用户输入生成唯一数量的问题并将输入收集到列表中
Generating Unique Numbers of Questions Based on User Input and Collecting input into a List
我想做什么:
询问用户他们想找到多少喜剧演员。
用户输入 5
然后在控制台中提示用户问题 1:“什么是喜剧演员编号[1]”
他们输入姓名,然后系统会提示“喜剧演员编号 [2] 是什么”,依此类推,直到输入喜剧演员编号 5...
最后,我想将这些输入收集到一个列表中以备后用。
到目前为止,代码是这样的:
Question = int(input("How Many Comedians Do You Want to Find?: "))
lst = list(range(1, Question + 1))
lst_of_input = []
while Question > 0 :
for i in range(len(lst)):
iterator = i
s = input("What is Comedian number {}?: ".format(*iterator))
if s == "Done":
break
lst_of_input.append(s)
print(lst_of_input)
有些元素是实验性的。为了停止 while 循环,我包括如果输入 Done 那么 while 循环将中断并且它将 return 输入的值。
我在 运行 时收到的错误是:
TypeError: format() argument after * must be an iterable, not int
为了消除错误并缩短您的代码,我建议您将它设为 for .. in range()
来循环喜剧演员。
最终代码如下所示:
Question = int(input("How Many Comedians Do You Want to Find?: "))
lst_of_input = []
for question in range(Question) :
s = input("What is Comedian number {}?: ".format(question+1))
if s == "Done":
break
lst_of_input.append(s)
print(lst_of_input)
我已经测试过了,它似乎工作得很好。
我想做什么:
询问用户他们想找到多少喜剧演员。
用户输入 5
然后在控制台中提示用户问题 1:“什么是喜剧演员编号[1]” 他们输入姓名,然后系统会提示“喜剧演员编号 [2] 是什么”,依此类推,直到输入喜剧演员编号 5...
最后,我想将这些输入收集到一个列表中以备后用。
到目前为止,代码是这样的:
Question = int(input("How Many Comedians Do You Want to Find?: "))
lst = list(range(1, Question + 1))
lst_of_input = []
while Question > 0 :
for i in range(len(lst)):
iterator = i
s = input("What is Comedian number {}?: ".format(*iterator))
if s == "Done":
break
lst_of_input.append(s)
print(lst_of_input)
有些元素是实验性的。为了停止 while 循环,我包括如果输入 Done 那么 while 循环将中断并且它将 return 输入的值。
我在 运行 时收到的错误是:
TypeError: format() argument after * must be an iterable, not int
为了消除错误并缩短您的代码,我建议您将它设为 for .. in range()
来循环喜剧演员。
最终代码如下所示:
Question = int(input("How Many Comedians Do You Want to Find?: "))
lst_of_input = []
for question in range(Question) :
s = input("What is Comedian number {}?: ".format(question+1))
if s == "Done":
break
lst_of_input.append(s)
print(lst_of_input)
我已经测试过了,它似乎工作得很好。