我需要一些幫助來處理我的代碼。我最近開始從朋友那裏學習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