【问题标题】:Generating Unique Numbers of Questions Based on User Input and Collecting input into a List根据用户输入生成唯一数量的问题并将输入收集到列表中
【发布时间】:2020-06-29 21:14:05
【问题描述】:

我想做的事:

询问用户他们想找到多少喜剧演员。

用户输入 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 循环将中断并返回输入的值。

我在运行时收到的错误是:

TypeError: format() argument after * must be an iterable, not int

【问题讨论】:

    标签: python python-3.x list list-comprehension string.format


    【解决方案1】:

    为了消除错误并缩短您的代码,我建议您将其设为 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)
    

    我已经测试过了,它似乎工作得很好。

    【讨论】:

    • 非常感谢!几天来,我一直在试图通过文档和堆栈溢出来解决这个问题,但我找不到任何东西。我真的很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多