【发布时间】:2022-07-06 21:45:19
【问题描述】:
我正在制作一个简单的杂货购买程序,它使用 2 个固定列表和 2 个可以包含用户添加元素的列表。
product_list = ['apple', 'orange', 'watermelon', 'banana', 'coconut']
price_list = [ 5.32 , 6.45 , 2.37 , 5.32, 6.45 ]
bought_product_list = [ ]
bought_price_list = [ ]
while True:
product_code = input('Enter the product code: ')
if product_code not in product_list:
print('Invalid product code! Try again!')
if product_code in product_list:
quantity = int(input('Enter the quantity:'))
for q in range(quantity):
bought_product_list.append(product_code)
我可以将 product_code 添加到购买的 price_list,但我需要一种方法来从其他列表中导入与 product_code 关联的正确价格。
假设用户输入数量为 2 的苹果和数量为 3 的西瓜,输出应该是:
bought_product_list = [ 'apple', 'apple', 'watermelon' , 'watermelon' , 'watermelon' ]
bought_price_list = [ 5.32 , 5.32 , 2.37 , 2.37 , 2.37 ]
有人可以帮我解答这个问题吗?
【问题讨论】:
标签: python