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!
Extend your presidential eligibility program to include a notice to the user that describes why they are not eligible to run. You should do the following in your program:

Ask the user for their age
Tell the user that they are too young if they are under 35 years old
Ask the user if they were born in the U.S.
Tell the user they need to be born in the U.S. to run for president if they answer ‘No’
Ask the user how many years they’ve been a resident of the U.S.
Tell the user they have not lived in the U.S. for long enough to run if they answer less than 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.
You are too young. You must be at least 35 years old.
… or like this:

Age: 40
Born in the U.S.? (Yes/No): No
Years of Residency: 5
You are not eligible to run for president.
You must be born in the U.S. to run for president.
You have not been a resident for long enough.
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

Sure! Below is a Python program that implements the requirements you described for determining presidential eligibility based on the user's age, citizenship, and residency.

# Ask the user for their age and store it in a variable
age = int(input("Age: "))

# Ask the user if they were born in the U.S.
born_in_us = input("Born in the U.S.? (Yes/No): ")

# Ask the user how long they’ve been a resident in the U.S.
years_of_residency = int(input("Years of Residency: "))

# Initialize eligibility status
eligible = True
eligibility_reasons = []

# Check age eligibility
if age < 35:
    eligible = False
    eligibility_reasons.append("You are too young. You must be at least 35 years old.")

# Check citizenship
if born_in_us != "Yes":
    eligible = False
    eligibility_reasons.append("You must be born in the U.S. to run for president.")

# Check residency duration
if years_of_residency < 14:
    eligible = False
    eligibility_reasons.append("You have not been a resident for long enough.")

# Final eligibility output
if eligible:
    print("You are eligible to run for president!")
else:
    print("You are not eligible to run for president.")
    # Print the reasons for not being eligible
    for reason in eligibility_reasons:
        print(reason)

How It Works

  1. The program prompts the user for their age, whether they were born in the U.S., and how long they’ve been a resident in the U.S.
  2. It verifies the user's eligibility to run for president based on the provided conditions:
    • At least 35 years old.
    • Must have been born in the U.S.
    • Must have been a resident for at least 14 years.
  3. If the user does not meet one or more of these conditions, the program collects reasons for ineligibility.
  4. Finally, it outputs whether the user is eligible or not and provides explanations for any conditions they did not meet.

Example Outputs:

  • If the user enters age 19, born in the U.S. as "Yes", and years of residency as 19, the output will be:
You are not eligible to run for president.
You are too young. You must be at least 35 years old.
  • If the user enters age 40, born in the U.S. as "No", and years of residency as 5, the output will be:
You are not eligible to run for president.
You must be born in the U.S. to run for president.
You have not been a resident for long enough.

Make sure to run this code in a Python environment to see how it works!