Write a program that awards olympians for winning gold medals

Write a program that awards olympians for winning gold medals. Let’s say there are five events today, and for every gold medal the winner receives $75,000. Prompt the user for how many gold medals the Olympian won. The Get_Winnings(m) function should take exactly one parameter—a string for the number of gold medals. It will return either an integer for the money won or a string Invalid, if the amount is invalid. Olympians can win one more medal per day.

The Correct Answer and Explanation is :

To create a Python program that calculates the earnings of an Olympian based on the number of gold medals won, we can follow these steps:

  1. Prompt the User: Ask the user to input the number of gold medals won.
  2. Validate the Input: Ensure the input is a valid integer within the acceptable range (0 to 5).
  3. Calculate Earnings: Multiply the number of valid gold medals by $75,000 to determine the total earnings.
  4. Handle Invalid Input: If the input is invalid (non-integer or out of range), return “Invalid”.

Here’s the Python code implementing this logic:

def Get_Winnings(m):
    try:
        # Convert the input string to an integer
        medals = int(m)
        # Check if the number of medals is within the valid range
        if 0 <= medals <= 5:
            # Calculate the total earnings
            earnings = medals * 75000
            return earnings
        else:
            return "Invalid"
    except ValueError:
        # Return "Invalid" if the input is not a valid integer
        return "Invalid"

# Prompt the user for input
medals_won = input("Enter the number of gold medals won today: ")

# Get the earnings or error message
result = Get_Winnings(medals_won)

# Display the result
print(result)

Explanation:

  • Function Definition: The Get_Winnings function takes one parameter m, which is expected to be a string representing the number of gold medals won.
  • Input Conversion and Validation: Inside the function, we attempt to convert m to an integer using int(m). If this conversion fails (i.e., if m is not a valid integer), a ValueError is raised, and the function returns “Invalid”.
  • Range Check: If the conversion is successful, we check if the number of medals is between 0 and 5 (inclusive). If it is, we calculate the earnings by multiplying the number of medals by $75,000. If the number is outside this range, the function returns “Invalid”.
  • User Interaction: The program prompts the user to enter the number of gold medals won, calls the Get_Winnings function with the user’s input, and prints the result.

Example Runs:

  1. Valid Input:
   Enter the number of gold medals won today: 3
   225000

In this case, the user entered 3, which is a valid number of medals. The function calculates 3 * 75000 = 225000 and returns this value.

  1. Invalid Input (Non-integer):
   Enter the number of gold medals won today: three
   Invalid

Here, the user entered a non-integer value. The int(m) conversion raises a ValueError, and the function returns “Invalid”.

  1. Invalid Input (Out of Range):
   Enter the number of gold medals won today: 6
   Invalid

The user entered 6, which is outside the valid range of 0 to 5. The function returns “Invalid”.

Additional Considerations:

  • Input Prompting: The prompt message clearly instructs the user to enter the number of gold medals won today.
  • Error Handling: The use of a try-except block ensures that the program gracefully handles invalid inputs without crashing.
  • Range Validation: The check 0 <= medals <= 5 ensures that only valid numbers of medals are accepted.

This program effectively calculates the earnings of an Olympian based on the number of gold medals won, handling various input scenarios appropriately.

Scroll to Top