![]() |
Finding average in dictionary using for loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Finding average in dictionary using for loop (/thread-18616.html) |
Finding average in dictionary using for loop - Rae - May-24-2019 So I'm a super newbie in python, and I'm taking a course right now and this is my homework. I'm supposed to get the average using a for loop, and I know I'm wrong but I don't know how to do it? :( Below is my code as of now! data = { 'Jeremy':73284, 'Hansel':8784.3, 'Uee':9480938.2, 'Seolhyun':984958.3, 'Ketsuno Ana':24131, 'Trump':45789 } total_networth = 0 total_number_of_people = 0 for row in data.items(): total_networth += networth total_number_of_people += 1 average = total_networth/total_number_of_people print("The average networth is $" + str(round(average,2))) RE: Finding average in dictionary using for loop - michalmonday - May-24-2019 You could check this out: https://www.w3schools.com/python/python_dictionaries.asp for value in thisdict.values(): print(value) for key, value in thisdict.items(): print(key, value) RE: Finding average in dictionary using for loop - ichabod801 - May-24-2019 items gives you the (key, value) pairs from the dictionary. You want the values method, which in your case would just give you the number values that you are trying to average. RE: Finding average in dictionary using for loop - Rae - May-26-2019 Thank you! RE: Finding average in dictionary using for loop - DeaD_EyE - May-26-2019 You can use the statistics module. A cool feature is, that you can use also Fraction. Math teacher will love this. import statistics data = { 'Jeremy':73284, 'Hansel':8784.3, 'Uee':9480938.2, 'Seolhyun':984958.3, 'Ketsuno Ana':24131, 'Trump':45789 } mean = statistics.mean(data.values()) median = statistics.median(data.values()) h_mean = statistics.harmonic_mean(data.values()) print("The mean networth is:", mean) print("The median networth is:", median) print("The h_mean networth is:", h_mean)You can use formatting (>= Python 2.7): print("The mean networth is: {:.2f}".format(mean)) print("The median networth is: {:.2f}".format(median)) print("The h_mean networth is: {:.2f}".format(h_mean)) And since Python 3.6 you could use Literal String Interpolation, better known as format strings. The syntax is equal, the only difference is, that the interpolation is accessing local variables.print(f"The mean networth is: {mean:.2f}") print(f"The median networth is: {median:.2f}") print(f"The h_mean networth is: {h_mean:.2f}") By the way, other functions you can use on the ValuesView , which is an Iterable .data = { 'Jeremy':73284, 'Hansel':8784.3, 'Uee':9480938.2, 'Seolhyun':984958.3, 'Ketsuno Ana':24131, 'Trump':45789 } dict_values = data.values() # the dict_values is the view to the values of the dictionary # it's accessing the original dict # if you update a value or add a key with a value # the view is updated print("Before Update") min_val, max_val = min(dict_values), max(dict_values) print(f"networth min: {min_val:.2f}\nnetworth max: {max_val:.2f}") # calculate and print values, before updating mean = statistics.mean(dict_values) median = statistics.median(dict_values) h_mean = statistics.harmonic_mean(dict_values) print(f"The mean networth is: {mean:.2f}") print(f"The median networth is: {median:.2f}") print(f"The h_mean networth is: {h_mean:.2f}") # updating the dict data["Angela Merkel"] = -666 print("After Update") min_val, max_val = min(dict_values), max(dict_values) print(f"networth min: {min_val:.2f}\nnetworth max: {max_val:.2f}") mean = statistics.mean(dict_values) median = statistics.median(dict_values) # h_mean = statistics.harmonic_mean(dict_values) # StatisticsError: harmonic mean does not support negative values print(f"The mean networth is: {mean:.2f}") print(f"The median networth is: {median:.2f}") |