Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix an empty output when using an if statement for a non integer variable?

I am currently a student who is struggling. OK, so I have a code that checks if you’re eligible to run for the presidency in the US. The problem is that the one and only non-number if statement question won’t give me an output if it doesn’t pass through the if statement. All the others work fine if I don’t input the right numbers to make me eligible except that one question. Here’s the code:

enter code here

age = int(input("Enter age: "))
    if age >= 35:
    age1 = 1
else:
    age1 = 0
us = input("Are you born in the US? (Y/N): ")
if us == "Y":
    us1 = 1
elif us == "N":
    us1 = 0
res = float(input("Years of residancy in the US: "))
if res >= 14:
    res1 = 1
else:
    res1 = 0
    if age1 + res1 + us1 == 3:
        print("You are eligible to run for president")
    else:
        print("You are not eligable to run for president.")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Python is a language in which indentation matters, the fact that you put the last if/else block inside the else of the last question makes so that it only executes when you input a residency < 14.

To solve this, put it outside such block, de-indenting all four rows.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading