简体   繁体   English

如何使用 while 循环将值存储在字典中并将它们显示为 python 3.0 中的表?

[英]How can I store values in a dictionary with a while loop and show them as a table in python 3.0?

So the code I'm doing right now as to create a catalogue, so first it asks the user how many items he wants to add to the catalogue and the to introduce 7 attributes to this item, being name, type, color, power, light type, price and units.所以我现在正在做的代码是为了创建一个目录,所以首先它询问用户他想添加多少项目到目录中,并为这个项目引入 7 个属性,即名称、类型、颜色、功率、灯的类型、价格和单位。 In addition to this I'm also storing the id (first item would be 1, second is 2, etc) and the price + taxes.除此之外,我还存储了 id(第一项是 1,第二项是 2,等等)和价格 + 税金。

lee_entero is a function that makes sures the number introduced is positive and an integer. lee_entero 是一个 function,它确保引入的数字是正数,一个 integer。

To do this i do the following:为此,我执行以下操作:

prod={}

l=[]

n= lee_entero("how many items? ")
        
        for i in range(1,n+1):
        
            p_nombre = input("Name: ")
            p_tipo = input("Type: ")
            p_color = lee_color("Color: ")
            p_potencia = lee_RealPositivo("Power(K): ")
            p_tipoLuz = lee_luz("Light type (amarillo, azul o blanco): ")
            p_precio = lee_RealPositivo("Price without taxes: ")
            if (p_precio <= 50):
             print("Low price ")
            elif (p_precio <= 100):
             print("Medium Price ")
            else:
             print("High price ")
            p_total = p_precio + p_precio*IVA/100
            p_unidades = lee_entero("Units: ")
            ids=i
           
            prod={"Product nº":ids, "name": p_nombre, "type": p_tipo, "color": p_color, "power": p_potencia, "light type": p_tipoLuz, "price": p_precio, "total": p_total, "units": p_unidades  }
            l.append(prod)
            i=i+1

And then I want this to be displayed as a table for which i have tried:然后我希望将其显示为我尝试过的表格:

print("{:<15} {:<15} {:<15} {:<15} {:<15} {:<15} {:<15} {:<15} {:<15}".format("id", "Nombre", "Tipo", "Color", "Potencia", "Tipo Luz", "Precio", "Precio IVA", "Unidades"))
           print("*******************************************************************************************************************************************************")

   for key, value in prod.items():

       elid, nombre, tipo, color, potencia, tipoluz, precio, precioiva, unidades = value

       print("{:<15} {:<15} {:<15} {:<15} {:<15} {:<15} {:<15} {:<15} {:<15}".format(elid, nombre, tipo, color, potencia, tipoluz, precio, precioiva, unidades))

but this doesn't work.但这不起作用。

Overall im looking for a way to store the values of each product and then display them as a table without importing any libraries, but right now is giving me the TypeError: cannot unpack non-iterable int object and I haven't been able to make it work, so any help is appreciated.总的来说,我正在寻找一种方法来存储每个产品的值,然后在不导入任何库的情况下将它们显示为表格,但现在给了我 TypeError: cannot unpack non-iterable int object 并且我无法制作它工作,所以任何帮助表示赞赏。

You are iterating over a dict, so using你正在迭代一个字典,所以使用

for key, value in prod.items():

will generate pair将生成对

key == "Product nº"
value == ids

in the first iteration.在第一次迭代中。 So you are trying to unpack value, which is int.因此,您正在尝试解压缩值,即 int。 To achieve what you want, to can do something like:要实现您想要的,可以执行以下操作:

for value in prod.values():
    print("{:<15}".format(value),end=" ")
print()

which will print all values from the dictionary without a new line, and then add a new line at the end这将打印字典中的所有值而无需换行,然后在末尾添加新行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何在 for 循环中交替键修改字典值? - Python: How can I modify dictionary values in a for loop alternating the keys? 如何将用户输入存储在 while 循环中? (Python) - How can I store the user input in a while - loop? (python) Python:如何用 while 循环替换这个 for 循环? (没有负值) - Python: How can I substitute this for loop with a while loop? (No negative values) 如何在字典中存储值 - How can i store values in my dictionary 我怎样才能让字典在不覆盖它们的情况下生成新的键和值 - How can i make a dictionary make new keys and values while not overwriting them 我如何从 2 个列表中获取值并将它们放入 python 中的字典中 - How can i get values from 2 lists and put them into a dictionary in python 嵌套循环字典存储值 python nsepy - nested for loop dictionary store values python nsepy 如何从字典 python 中的 while 循环更新值(列表形式) - How to Update values (List forms) from while loop in dictionary python 如何使用 while 循环从用户输入中附加具有任意数量的键和多个值的字典 - How can I use a while loop to append a dictionary with an arbitrary number of keys and multiple values from user inputs 如何在while循环语句中引用具有布尔项作为值的字典? - How can I reference a dictionary with boolean terms as values in a while-loop statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM