【问题标题】:How to add two inputs and save them as lists of lists continuously?如何添加两个输入并将它们连续保存为列表列表?
【发布时间】:2021-03-11 18:03:45
【问题描述】:

例如,您必须取两个值,一个是商品名称,另一个是商品价格。并且您希望将该信息存储在与列表列表相同的列表中。 假设香蕉的价格是 10,苹果是 20,橙子是 30,那么列表应该是这样的:

[(banana , 10) , (apple , 20) (orange , 30) ].

请注意,我们会要求用户输入这两个值。

【问题讨论】:

标签: python python-3.x list python-requests


【解决方案1】:

您可以通过创建空列表然后将用户输入附加到此列表来实现此目的。 使用 while 循环用户可以提供不止一种水果及其价格。 通过定义退出值'quit',你可以告诉程序何时停止while循环。

lst = []
while True:
    fruit = input('Enter Fruit or if you want to quit type ''quit'' : ')
    if fruit == 'quit':
        break
    price = int(input('Enter price : '))
    lst.append([fruit, price])

print(lst)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2021-02-05
    • 1970-01-01
    相关资源
    最近更新 更多