Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IF statement Issues
#1
#hey everyone I am a new student in Python. Below is some code working on IF statements. Please
#refer to the comment question in the middle of the page.

def main():
age = eval(input("Enter your age: "))
citizen = eval(input("Enter the amount of years you have been a citizen: "))
if age >= 30:
if citizen >= 9:
print("You can be a state senator!")
if age >= 25:
if citizen >= 7:
print("You can also be a house representative!")

#I understand that ELSE will only show up if the other IF statements are not triggered
#but how would one arrange the code to allow ELSE to be triggered if an IF statement is
#also being used?
else:
print("You are not eligible for senate.")
print("You are also not eligible for the house.")
main()


#Please see previous comments
Reply
#2
Especially with function definitions and if statements, indentation is essential in Python. Please repost your code with python tags so we can see the indentation. See the BBCode link in my signature below for instructions.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It is indented when I paste it in but when I post it the indentations aren't there.

def main():
    age = eval(input("Enter your age: "))
    citizen = eval(input("Enter the amount of years you have been a citizen: "))
    if age >= 30:
        if citizen >= 9:
            print("You can be a state senator!")
    if age >= 25:
        if citizen >= 7:
                print("You can also be a house representative!")
        
    #I understand that ELSE will only show up if the other IF statements are not triggered
    #but how would one arrange the code to allow ELSE to be triggered if an IF statement is
    #also being used?
    else:
        print("You are not eligible for senate.")
        print("You are also not eligible for the house.")
main()
    

This is a result I got from a specific input:

Enter your age: 25
Enter the amount of years you have been a citizen: 7
You can also be a house representative!
Reply
#4
Did you use python tags like I suggested? Did you read the instructions in the BBCode link I mentioned?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I don't really understand the question you posed in your comment before the else.

If statements can be nested, as you have done. If statements that follow one another (not nested, but at the same level) do not take account of any previous condition evaluations.

You can set "flags" within if blocks that make it easier to to take account of something previously tested without having to repeat a complex test. A flag is just a boolean variable, set to True or False.

Don't forget, you can also combine conditions in the test: a => 1 and a <= 10.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#6
I'm not sure exactly what output you're looking for. But I'm thinking you want a separate else statement for each of the if statements that use citizen.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
I should probably change my ELSE output but it should say I will not be able to be a senator. How would I add an else tag for just that so it still says I can be in the house of representatives.
Reply
#8
Each if can have it's own else.
>>> spam = 42
>>> if spam > 5:
...   if spam > 10:
...     if spam > 100:
...       print("it's big")
...     else:
...       print("it's a little big")
...   else:
...     print("larger than 5, less than 10")
... else:
...   print("less than 5")
...
it's a little big
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020