我需要一些帮助来处理我的代码。我最近开始从朋友那里学习Python。通常,如果我遇到问题,他会告诉我该怎么做,但他休假几周,所以他不能帮忙。我需要创建一个价格比较工具来比较产品的价格。我可以完成大部分代码,我只需要帮助我们如何从列表中获取内容。我可以收集所有信息并将其放入列表中,但我不确定如何再次将它取出。我需要它显示所有输入产品的平均单价,最便宜的整体产品,最昂贵的整体产品以及用户设置的预算中最便宜单价的建议。我不知道如何从列表中收集此信息。任何帮助,将不胜感激。这是我的代码到目前为止:(我已经遗漏了功能)从列表中获取信息
keep_going = ""
while keep_going == "":
sum_table = []
count = 0
unit_type = ""
print("Welcome to the price comparison tool!")
print()
how_much = num_check("How much money do you have to spend? $", float, 1, 100)
get_prod = True
while get_prod:
if count < 1:
p_name = len_check("What is the name of the first product? ")
elif 0 < count:
p_name = len_check("Please enter another product, or type XXX to bring up summary ")
if p_name.lower() == "xxx":
break
unit = num_check("Is the product in g/ml or kg/L? (enter 1 for g/ml or 2 for kg/l) ", int, 1, 2)
if unit == 1:
unit_type = "grams/millilitres"
elif unit == 2:
unit_type = "kilograms/litres"
p_mass = num_check("What is the mass of '{}' in {}? " .format(p_name, unit_type), float, 1, 1000)
if unit == 2:
p_mass = p_mass*1000
p_price = num_check("What is the price of '{}' in dollars? $" .format(p_name), float, 0.1, 100)
p_average = p_price/(p_mass/1000)
row = [p_name, p_mass, p_price, p_average]
sum_table.append(row)
count += 1
for i in sum_table:
if i[2] > how_much:
sum_table.remove(i)
print()
print("--- Product Summary ---")
print("All items over-budget have been removed! ")
print("Name\tMass in g/ml\tPrice\tUnit price per kg")
for i in sum_table:
print(i)
print()
keep_going = input("Press <enter> to go again or any other key to quit")
print()
考虑添加注释你的代码解释自己在做什么,或者你有 – Cuber
什么问题还有,什么是'len_check()'和'num_check()'? –
有很多内置的功能(最小,最大,总和等),它们可以完成你想要实现的功能,只需将它们放到谷歌地图上,你就会发现很多信息。 – coder