Write a program that reports whether or not someone is eligible to run for president in the U.S. You should do the following in your program:

Ask the user for their age, and store it in a variable
Ask the user if they are a citizen of the U.S.
Ask the user how long they’ve been a resident in the U.S.
Use an if/else statement with the proper comparison operator to print You are eligible to run for president! if they have the following credentials:
They are at least 35 years old
They were born in the U.S.
They have been a resident for at least 14 years
An example run of your program might look like this:

Age: 19
Born in the U.S.? (Yes/No): Yes
Years of Residency: 19
You are not eligible to run for president.
… or like this:

Age: 40
Born in the U.S.? (Yes/No): Yes
Years of Residency: 20
You are eligible to run for president!
Note:
Remember that capitalization is very important! Make sure to check if the user replied ‘Yes’ or ‘No’, not ‘yes’ or ‘no’. You will not pass the autograder if your capitalization is off!

4.4.5 on code.hs

1 answer

Here is a Python program that checks if someone is eligible to run for president in the United States based on the criteria you've provided:

# Function to check presidential eligibility
def check_presidential_eligibility():
    # Ask for the user's age
    age = int(input("Age: "))
    
    # Ask if the user was born in the U.S.
    born_in_us = input("Born in the U.S.? (Yes/No): ")
    
    # Ask for the number of years they've been a resident
    years_of_residency = int(input("Years of Residency: "))
    
    # Determine eligibility
    if age >= 35 and born_in_us == "Yes" and years_of_residency >= 14:
        print("You are eligible to run for president!")
    else:
        print("You are not eligible to run for president.")

# Call the function to execute the program
check_presidential_eligibility()

How the program works:

  1. It prompts the user for their age and converts it to an integer.
  2. It asks whether the user was born in the U.S. and stores the response as a string.
  3. It requests the number of years the user has been a resident in the U.S. and converts that to an integer.
  4. Using an if/else statement, it checks if the user meets the necessary criteria to run for president.
  5. It prints a message based on whether the user is eligible or not.

Important Notes:

  • Ensure you run this code in an environment that supports Python input/output.
  • The program checks for exact matches in capitalization, ensuring that only 'Yes' or 'No' are valid responses.