gpt4 book ai didi

python - 如何使用列表中的值作为对Python中另一个值的引用?

转载 作者:太空宇宙 更新时间:2023-11-03 16:42:21 25 4
gpt4 key购买 nike

我正在使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com