def get_count(prompt):
"""Ask until the user types a whole number from 1 to 50."""
while True:
text = input(prompt).strip()
try:
n = int(text)
except ValueError:
print("Please type a whole number, like 3.")
continue
if n < 1 or n > 50:
print("Please enter between 1 and 50 coats.")
else:
return n
def add_donation(rooms, counts, room, n):
"""Add n coats to a homeroom, creating it if it is new."""
room = room.strip().upper()
for i in range(len(rooms)):
if rooms[i] == room:
counts[i] = counts[i] + n
return
rooms.append(room)
counts.append(n)
def leaderboard(rooms, counts):
"""Return [count, room] pairs, most coats first."""
pairs = []
for i in range(len(rooms)):
pairs.append([counts[i], rooms[i]])
pairs.sort(reverse=True)
return pairs
def show_leaderboard(rooms, counts):
print("--- Coat Drive Leaderboard ---")
place = 1
for pair in leaderboard(rooms, counts):
print(f"{place}. Room {pair[1]}: {pair[0]} coats")
place = place + 1
print("Total:", sum(counts), "coats")
def main():
rooms = []
counts = []
while True:
print("1. Add coats 2. Leaderboard 3. Quit")
choice = input("Choose 1-3: ").strip()
if choice == "1":
room = input("Homeroom (e.g. 11B): ")
n = get_count("Number of coats (1-50): ")
add_donation(rooms, counts, room, n)
print("Thank you! Recorded.")
elif choice == "2":
show_leaderboard(rooms, counts)
elif choice == "3":
print("Goodbye!")
break
else:
print("Please type 1, 2 or 3.")
main()