Reputation: 1
I'm trying to get my code to print "the lowest score of (lowest score) was by the judge from (a country with the lowest score)" from a list of scores
Here's my code so far:
print("ASSIGNMENT 02B - Olympic Scores\n")
print("This program prompts the user to enter seven scores\nbetween 0.0 and 10.0 from seven different national judges.\nThe program will determine the high and low scores along\nwith which judge gave those scores. The high and low scores are\nomitted and the remaining five (middle) scores are averaged\nand the result displayed.")
print("-")
chi=int(input("Enter the judge's score from China: "))
fra=int(input("Enter the judge's score from France: "))
ger=int(input("Enter the judge's score from Germany: "))
bri=int(input("Enter the judge's score from Great Britain: "))
swe=int(input("Enter the judge's score from Sweden: "))
usa=int(input("Enter the judge's score from the United States: "))
zim=int(input("Enter the judge's score from Zimbabwe: "))
scores=[chi,fra,ger,bri,swe,usa,zim]
scores.remove(min(scores))
scores.remove(max(scores))
scoresum=sum(scores)
ave=(scoresum)/5
###
print("")
print("The average score of the middle five judges was " + str(ave))
Upvotes: 0
Views: 94
Reputation: 1820
Instead of holding scores in a list, you can make use of a dictionary which is quite useful to find who scored the max and min values.
Here is the code:
scores = {
'China': 0,
'France': 0,
'Germany': 0,
'Great Britain': 0,
'Sweden': 0,
'United State': 0,
'Zimbabwe': 0
}
for judge in scores:
scores[judge] = int(input(f"Enter the judge's score from {judge}: "))
scores = sorted(scores.items(), key=lambda item: item[1])
min_score = scores.pop(0)
max_score = scores.pop(-1)
avg_score = sum(i[1] for i in scores)/len(scores) # or simply put 5, if judges count is fixed
print(f"The average score of the middle five judges was {avg_score}")
print(f"Max score of {max_score[1]} gave a judge from {max_score[0]}")
print(f"Min score of {min_score[1]} gave a judge from {min_score[0]}")
Explanation:
We create a scores
dictionary that holds judges and their score which is set to 0
initially.
Next, we iterate through the keys of scores
and ask a user to input judges' scores.
In order to find min and max scores we use built-in sorted
function on scores.items()
which returns tuple like ((key, value), (key2, value2))
and sort this tuple by its values using the inline function lambda item: item[1]
.
Now, we already know that the first element is the minimum score, and the last element is with maximum score. We pop
these tuples to variables min_score
and max_score
.
We calculate the average score, we get values by i[1] for i in scores
and sum()
them, later dividing them into the length of the tuple
Lastly, we print our data.
Hope that helps, if it solved your problem, don't forget to upvote and mark it as an 'Accepted Answer' :)
Upvotes: 0