[英]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.