r/codehs • u/gigapudding43201 • Apr 07 '21
4.4.5 Help
years=input("Age:")
citizen= input("Born in the U.S.? (Yes/No):")
residency= input("Years of Residency:")
if years >= str(35) and citizen == "yes" and residency >= str(14):
print("You are eligible to run for president!")
else:
print("You are not eligible to run for president. ")
Getting an error. The program says that my code is not displaying "You are not eligible to run for president" when it does. Help?
2
Apr 07 '21
so, problem is you are trying to compare numbers using strings. That usually cause errors, because strings don't have "number values". For that, you wanna use the "int" function, which will take a string input and take it to a numeric value. Example:
years=input("Age:") #bad, "years" should be a number, not string
years=int(input("Age:")) #good, now "years" is a number
If It's confusing, you can also get the string input and then convert to int, like:
years_string = input("Age:") # First I got the string input, for example: "37"
years_value = int(years_string) # Now I take the "37" and take It's numerical value, 37
However, the first one is usually better.
Let me know If It's clear, now. You should be able to solve the problem by correctly applying these to all variables that are numbers, and not strings.
1
1
1
u/UndetailedCake Oct 24 '23
Here
age = int(input("Age: "))
if age >= 35:
print ("You are eligible to run for president.")
else:
print ("You are not eligible to run for president.")
3
u/Idk_idk_what_idk Oct 09 '22
age = int(input("What is your age? :"))
born = input("Were you born in the U.S? :")
resident = int(input("How long have you lived in the U.S? :"))
print("Age:" + str(age))
print("Born in the U.S.? (Yes/No):" + born)
print("Years of Residency: " + str(resident))
if age >= 35 and born == ("Yes") and resident >= 14:
print("You are eligible to run for president!")
else:
print("You are not eligible to run for president")
Here