【问题标题】:Counting inputs with lists用列表计算输入
【发布时间】:2015-02-19 02:16:07
【问题描述】:

最终,我想构建一个脚本,它接受用户的输入,不仅计算他们有多少输入,而且在最后的语句中重复它们。该脚本应该询问用户他们想将什么放入篮子中。用户满意后,他们可以输入“nothing”,这将停止循环并返回类似“篮子里有 x 个项目:item1、item2、item3”......等等。

我不知道如何计算每个输入,然后在最后一行使用确切的输入。

这是我目前所拥有的:

print('Add as many items to the basket as you want. When you are done, enter "nothing".')
print('What do you want to put into the basket now?')
while True:
    myInput = input()
    variable = 0
    if myInput == "nothing":
        print('There are' + str(variable) + ' items in the basket: [' + x + '.]')
        break
    else:
        print('Okay, what else?')
        variable += 1

如果有人能指出我正确的方向,那就太好了。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:
    print('Add as many items to the basket as you want. When you are done, enter "nothing".')
    print('What do you want to put into the basket now?')
    #create basket list
    basket = []
    while True:
        myInput = input()
        if myInput == "nothing":
            #print length of basket to count elements, and use basket to show content
            print('There are ' + str(len(basket)) + ' items in the basket: '+ str(basket))
            break
        else:
            # add input to basket
            basket.append(myInput)
            print('Okay, what else?')
    

    在快速测试时,当用户输入为 '' 时出现错误,
    这意味着他们没有输入任何字符并按下回车键。

    我会把这个错误留给你去解决。

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      相关资源
      最近更新 更多