Home > Software design >  weird results from python if statement, please help me with this
weird results from python if statement, please help me with this

Time:11-05

here is my code which is producing some weird results when input value is changed. ps I know that there are lot of comments in it, I was using that as a means of debugging.

age = 0

age = input("please enter your age ")
#print(age)
#int(age)
#print(type(age))
age1 = int(age)
#print(type(age1))
print(age1)
if age1 > 30:
    #ageV = "old"
    print("old")
else: age1 < 30
#ageV  = "young"
print("young")
#print(ageV)

Any suggestions and explanation about what is going on would be gratefully received.

CodePudding user response:

You have an error in line else: age1 < 30 second condition is not needed, just write this part as:

if age1 > 30:
    print("old")
else:
    print("young")

CodePudding user response:

There are only two possibilities here:

  1. Age is greater than 30

  2. Age is equal to or less than 30

So, you only need one if statement, and the 2nd possibility will be executed under else, like so:

if age1 > 30: 
 print("Old")
else: 
 print("Young")
  • Related