Incorrect Output results displaying for some values in a python programme

Viewed 26

I have written a python program in Visio code and I found a strange Behavior

code:

a=input("Enter Jim age ") 

b=input("Enter Michal age ") 

c=input("Enter sunny age ") 

d=input("Enter Anders age ") 

if(a>b):     
    print("Jim is elder") 

if(b>c):     
    print("Michal is elder")  

if(c>d):     
    print("sunny is elder") 
else:     
    print("nothing is true")

Now when I input values 13,11,9,8 it will display only "Jim is elder sunny is elder" which is incorrect. But if I put the input values 20,18,16,14 ,it will display all the results correctly. The same incorrect results also showing when I input 11,9,8,7 in which case output is " Michal is elder sunny is elder".

2 Answers

You need convert the input string to int before comparing them like so:

a= int(input("Enter Jim age ")) 

b= int(input("Enter Michal age ")) 

c= int(input("Enter sunny age "))

d= int(input("Enter Anders age "))

if(a>b):     
    print("Jim is elder") 

if(b>c):     
    print("Michal is elder")  

if(c>d):     
    print("sunny is elder") 
else:     
    print("nothing is true")

Try this

a=int(input("Enter Jim age "))

b=int(input("Enter Michal age "))

c=int(input("Enter sunny age "))

d=int(input("Enter Anders age "))

if(a>b):
  print("Jim is elder")

if(b>c):
  print("Michal is elder")

if(c>d):
  print("sunny is elder") 

else:
  print("nothing is true")
Related