我有三个清单:"产品","价格"和"项目编号"。 当用户输入被添加到这些列表中时,我如何让Python创建一个关联,让我们说"产品6,价格6和项目编号6"?
我希望能够看到的是,当用户从表中选择一个项目编号时,它会被存储,而项目编号实际上知道为自己分配产品的价值和价格。
Jay's House of Rip-Offs
Item Number |Item Name |Price
--------------------------------------------------
1 |Notebook |4.99
2 |Atari |99.99
3 |TrapperKeeper |89.99
4 |Jeans |3.99
5 |Insects |2.99
6 |Harbormaster |299.99
7 |Lobotomy |19.99
8 |PunkRock |3.99
9 |HorseFeathers |4.99
10 |Pants |2.99
11 |Plants |119.99
12 |Salami |1.99
Order products [Y / N]?:
这是我的代码,我真的不知道该怎么做才能让它发挥作用。我需要用户输入来存储'订单'和'数量'。此外,名称和地址输入的输入需要存储在上面的相应列表中。
products = ['Notebook', 'Atari', 'TrapperKeeper', 'Jeans', 'Insects',
'Harbormaster', 'Lobotomy', 'PunkRock', 'HorseFeathers', 'Pants',
'Plants', 'Salami']
prices = ['4.99', '99.99', '89.99', '3.99', '2.99', '299.99', '19.99',
'3.99', '4.99', '2.99', '119.99', '1.99']
item_nums = [1, 2, 3, 4, 5, 6, 7, 8 ,9, 10, 11, 12]
orders = []
quantity = []
response = ''
cust_name = ''
street = ''
city = ''
state = ''
zipcode = 0
order_total = 0
order_summary = ''
print("Jay's House of Rip-Offs\n\n")
titles = ('Item Number', 'Item Name', 'Price')
data = [titles] + list(zip(item_nums, products, prices))
for i, d in enumerate(data):
line = '|'.join(str(x).ljust(16) for x in d)
print(line)
if i == 0:
print('-' * len(line))
while str(input("Order products [Y / N]?: ")) != 'N':
item_nums = input("Enter an item number: ")
orders.append(item_nums)
quantity = input("How many? ")
if len(item_nums) == 0:
print("Thank you for browsing.")
else:
cust_name = input("Enter name: ")
street = input("Enter street address: ")
city = input("Enter city or town name: ")
state = input("Enter state or province: ")
zipcode = input("Enter zipcode: ")
答案 0 :(得分:0)
如果您不想使用类如何使用嵌套字典来存储数据?可能的实现可能如下所示:
products = {1: {"Type": "Notebook", "Price": 4.99}, 2: {"Type":
"Atari", "Price": 99.99}}
print(products[1])
> {'Type': 'Notebook', 'Price': 4.99}