【发布时间】:2017-04-21 06:10:11
【问题描述】:
我正在努力做到这一点,以便我可以将每个成本乘以相应的销售产品数量。例如,1.99 * 10、1.49 * 5 等等。另外,我似乎无法弄清楚如何根据列表中的成本打印出最昂贵或最便宜产品的产品名称。我尝试将 product_cost 中的 i 与 product_sold 的相应 i 相乘,但答案似乎很遥远。有谁知道如何解决这个问题?谢谢
但是,使用下面的代码,
# product lists
product_names = ["prime numbers", "multiplication tables", "mortgage calculator"]
product_costs = [1.99, 1.49, 2.49]
product_sold = [10, 5, 15]
def report_product():
total = 0
print("Most expensive product:", max(product_costs))
print("Least expensive product:", min(product_costs))
for i in range(len(product_costs)):
total += i * product_sold[i]
print("Total value of all products:", total)
selection = ""
while selection != "q":
selection = input("(s)earch, (l)ist, (a)dd, (r)emove, (u)pdate, r(e)port or (q)uit: ")
if selection == 'q':
break
elif selection == 's':
search_product()
elif selection == "l":
list_products()
elif selection == "a":
add_products()
elif selection == "r":
remove_products()
elif selection == "u":
update_products()
elif selection == "e":
report_product()
else:
print("Invalid option, try again")
print("Thanks for looking at my programs!")
【问题讨论】:
-
离题:
if selection == 'q': break是多余的,因为 while 循环的条件是while selection != 'q'无论如何都会使循环中断 -
能否显示 search_product、list_products、remove_products、update_products、report_product 功能的代码..??
标签: python list python-3.x function