python - Getting information from a list -
i need code. started learning python friend. usually, if have problem show me do, out on holiday few weeks, can not help. need create price comparison tool compare prices of products entered. can of code, need how stuff out of list. can gather info , put list, i'm not sure how out again. need display average unit price of products entered, cheapest overall product, expensive overall product , recommendation cheapest unit price in budget user sets. i'm not sure how gather info inside list. appreciated. code far: (i have left out functions)
keep_going = "" while keep_going == "": sum_table = [] count = 0 unit_type = "" print("welcome price comparison tool!") print() how_much = num_check("how money have spend? $", float, 1, 100) get_prod = true while get_prod: if count < 1: p_name = len_check("what name of first product? ") elif 0 < count: p_name = len_check("please enter product, or type xxx bring summary ") if p_name.lower() == "xxx": break unit = num_check("is product in g/ml or kg/l? (enter 1 g/ml or 2 kg/l) ", int, 1, 2) if unit == 1: unit_type = "grams/millilitres" elif unit == 2: unit_type = "kilograms/litres" p_mass = num_check("what mass of '{}' in {}? " .format(p_name, unit_type), float, 1, 1000) if unit == 2: p_mass = p_mass*1000 p_price = num_check("what 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 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") in sum_table: print(i) print() keep_going = input("press <enter> go again or other key quit") print()
in way showing it, different items, can pull each item out of row.
for example:
>>> row = [1,2,3,4,5] >>> a,b,c,d,e = row >>> print a,b,c,d,e 1 2 3 4 5
thus loop on rows , use p_name, p_mass, p_price, p_average = row[i]
now (as example), set max-p-index index of row showing maximum price. , max-p actual maximum price value. can similar calculations every value want keep track of, building averages. once finish loop, have values need appropriately.
if max-p < p_price: max-p = p_price max-p-index =
you can cheapest price, unit prices, averages, etc in same way within loop on row after have removed overpriced rows.
Comments
Post a Comment