【问题标题】:How to create a new list from a user's input in Python 3?如何从 Python 3 中的用户输入创建新列表?
【发布时间】:2018-09-24 23:38:52
【问题描述】:

我一直在尝试在这个问题上找到自己的方式,但到目前为止还没有找到任何答案。当用户被要求input 一个字符串时,我正在尝试创建一个新的list。我真的可以在这里应用list 命令还是有更好的方法?只是 Python 的初学者。

我的程序会询问有多少库存(例如水果、蔬菜、饮料)。 然后它会询问库存清单的名称。 对于库存的每个名称,它们将包含每个项目的项目,并要求用户输入该特定库存中有多少项目。

这是我的代码:

# MODULES
import os

# FUNCTIONS


def clear():
    os.system('cls')


def skip():
    input("<Press Enter To Continue>")


def createINV():
    clear()
    invlist = []

    countinv = int(input("Enter the number of Inventories: "))

    for x in range(countinv):
                                                                                            # ADD LISTS HERE
       # print(("Enter Inventory #%d" % (x+1)), ":")

        #typeinv = input(" :")
        addinv = input("Enter Inventory #%d: " % (x+1))
        invlist.append(addinv)
        # invlist.index(x) = []                < ------ function call so i can't assign here



        for y in range(countinv):
            countitems = int(input("\nHow many items in %s: " % invlist[y]))
            for z in range(countitems):
                additem = (input("Enter item#%d for %s Inventory: " % ((z+1), invlist[y])))
                invlist.index(x).append(additem)             # <--- this is where my program stops

            #print(invlist[x])
    #for x in range(countinv)

                                                                                            # PRINT LISTS HERE

    for x in range(countinv):
        print("\n", invlist[x])

    # for x in range(countinv):
    #     print(invlist[x])


# START - Welcome
clear()
print("Hi! Welcome to Python Inventory System.")
skip()
clear()

# START - Introduction
print("This is an Inventory System where you can input any Inventories you want and how many you want.")
print("For e.g.: You can input 3 types of Inventories such as Vegetables, Fast Foods, Drinks, etc.")
print("And you can input the prices for each item.")
skip()
clear()


# COMMENCE PROGRAM
x = 0
while x != 1:
    start = input("Are you ready to Start? (Y/N):")
    if start == 'y' or start == 'Y':
        x += 1
        createINV()

    elif start == 'n' or start == 'N':
        x += 1
        clear()

    else:
        x = 0

【问题讨论】:

    标签: python-3.x list dictionary for-loop input


    【解决方案1】:

    我建议你试试字典,不会透露完整的代码来让你的程序工作,学习它,如果你打算大量使用数据,它会很有用。

    inventories = dict() #initiate empty dictionary.
    
    QTY = int(input("How many inventories? "))
    
    for x in range(QTY): 
        name = input("Name of your inventory #{0}/{1} ".format(x+1, QTY)
        inventories[name] = dict()
    

    此示例将创建您的初始清单。

    您可以重复使用此代码以相同的方式在您的库存中创建项目。

    因此,您的主词典将充当其他词典的列表,而这些词典又是您存储项目的列表,具有您想要分配给它们的任意数量的属性。

    【讨论】:

    • 感谢您的想法。在我把它贴在这里之前,我已经分析了一天。我尝试了很多东西,但似乎没有任何效果,我在兜圈子。无论如何,这里的这一行“inventories[name] = dict()”,如果这行得通,那么这就是我一直在寻找的关键。我只需要一个可以迭代的字符串,我可以将其用于分配。谢谢,我会尽快向您发送更新。
    • 它会起作用 :-) 请记住,您的每个库存都是另一个字典。以及项目,因为它们有名称和数据
    • 您的最终结果将类似于:inventories{ 'fruits':{ 'peaches':{'qty':2, 'price':2.99}, 'strawberries':{'qty': 4, 'price':1.45} }, 'drinks':{ 'somedrink':{'qty':x, 'price':y}}}
    • 这正是我想要的输出。这被证明是困难的,我可能需要在使用字典方面做更多的研究。我能够显示我的项目列表(即 dict),但它们都显示为每个库存(即列表)。现在也许我需要休息一下。
    • 请问您如何将用户的输入分配给字典的第一个键?我的代码是'invlist.keys[a] = additem'? 'a' 是循环计数,additem 是用户输入的变量。它不起作用,它说“dict is not callabe”似乎在任何地方都找不到类似的例子。
    猜你喜欢
    • 1970-01-01
    • 2018-01-09
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多