我正在使用Python,所以我想知道如何编写一个读取代码、价格和多种产品数量的程序。 然后程序应该打印成本较高的产品的代码和单位较少的产品的代码。 这是我尝试做的:
Price=[]
code=[]
quantity=[]
Num=x
for i in range(x):
code.append(input("product code: "))
price.append(float(input("price: ")))
quantity.append(int(input("quantity: ")))
print(code[max(price)])
print(code[min(quantity)])
谢谢了!
最佳答案
一些可能会给您带来麻烦的项目:
价格 = []
应为价格 = []
Num=x
应为x=10
或其他数字for
下的所有三行都需要具有相同的缩进。
要获取最高价格的代码和最低数量的代码,您需要找到最高和最低的索引并在代码列表中使用该索引:
print(code[price.index(max(price))])
print(code[quantity.index(min(quantity))])
关于python - 如何使用列表中的值作为对Python中另一个值的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36683824/