Dexter
Dexter

Reputation: 139

python code running else statement even after if statement is true

I am learning python programming and was going through If Else conditions. Even if the If statement is true, my code executes else condition.

Please check the code below:

age = int(input("Enter your Age (in years)"))
sex = input("Enter you Sex(M/F)")
if(sex == 'M'):
    if(age < 20):
        print("You are just a teen.")
    if(age >= 20 and age < 25):
        print("You are a young man now.")
    elif(age >=25 and age < 30):
        print("You are a mature man now")
    else:
        print("You are getting old")
if(sex == 'F'):
    if(age < 20):
        print("You are just a teen.")
    if(age >= 20 and age < 25):
        print("You are a young woman now.")
    elif(age >=25 and age < 30):
        print("You are a lady now")

Over here, if i enter the age as 2 and sex as M, the code goes in first condition and prints the message

"You are just a team"

Along with it, the code also run the else condition and prints

You are getting old

I don't understand this behaviour. I checked for indentation and all indentations are correct.

Upvotes: 0

Views: 5345

Answers (4)

s.singh
s.singh

Reputation: 62

It is printing correct output. First, it is checking that age is less than 20 years, which is correct, it then print "You are just a teen.".

if(sex == 'M'):
    if(age < 20):
        print("You are just a teen.")

After that it checks second 'if' statement, then 'elif' and then it goes to 'else' and print that statement as there was no match previously.

if(age >= 20 and age < 25):
    print("You are a young man now.")
elif(age >=25 and age < 30):
    print("You are a mature man now")
else:
    print("You are getting old")

You may have made a typo here:

if(age >= 20 and age < 25):
    print("You are a young man now.")

Possibly, you are trying to use 'if' instead of 'elif' here.

Upvotes: 1

Melody
Melody

Reputation: 41

In the code snippet you've given, the else is connected to the second if statement: if(age >= 20 and age < 25):. The first "if" executes fine, but then when the second "if" fails it executes the "else". This can be fixed by changing the second "if" to an "elif":

if(sex == 'M'):
    if(age < 20):
        print("You are just a teen.")
    elif(age >= 20 and age < 25):
        print("You are a young man now.")
    elif(age >=25 and age < 30):
        print("You are a mature man now")
    else:
        print("You are getting old")

Upvotes: 1

nulltron
nulltron

Reputation: 646

You accidentally made it a double if which would cause both statements to execute.

age = int(input("Enter your Age (in years)"))
sex = input("Enter you Sex(M/F)")
if(sex == 'M'):
    if(age < 20):
        print("You are just a teen.")
    elif(age >= 20 and age < 25): # notice now it is one if-elif block
        print("You are a young man now.")
    elif(age >=25 and age < 30):
        print("You are a mature man now")
    else:
        print("You are getting old")
if(sex == 'F'):
    if(age < 20):
        print("You are just a teen.")
    elif(age >= 20 and age < 25): # same here
        print("You are a young woman now.")
    elif(age >=25 and age < 30):
        print("You are a lady now")

Upvotes: 3

Primusa
Primusa

Reputation: 13498

Switch

if(age < 20):
    print("You are just a teen.")
if(age >= 20 and age < 25):
    print("You are a young man now.")

with

if(age < 20):
    print("You are just a teen.")
elif(age >= 20 and age < 25):
    print("You are a young man now.")

What's happening is that your second if statement inside of if sex == 'M' isn't getting fulfilled because the age is not between 20 and 25. Since the elif isn't fulfilled either, whats inside the else block runs.

Upvotes: 1

Related Questions