Comparing numbers give the wrong result in Python

If I enter any value less than 24, it does print the “You will be old…” statement. If I enter any value greater than 24 (ONLY up to 99), it prints the “you are old” statement.

The problem is if you enter a value of 100 or greater, it prints the “You will be old before you know it.” statement.

print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if myAge > ('24'):
     print('You are old, ' + myName)
else:
     print('You will be old before you know it.')

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You’re testing a string value myAge against another string value '24', as opposed to integer values.

if myAge > ('24'):
     print('You are old, ' + myName)

Should be

if int(myAge) > 24:
    print('You are old, {}'.format(myName))

In Python, you can greater-than / less-than against strings, but it doesn’t work how you might think. So if you want to test the value of the integer representation of the string, use int(the_string)

>>> "2" > "1"
True
>>> "02" > "1"
False
>>> int("02") > int("1")
True

You may have also noticed that I changed print('You are old, ' + myName) to print('You are old, {}'.format(myName)) — You should become accustomed to this style of string formatting, as opposed to doing string concatenation with + — You can read more about it in the docs. But it really doesn’t have anything to do with your core problem.

Method 2

The string '100' is indeed less than the string '24', because '1' is “alphabetically” smaller than '2'. You need to compare numbers.

my_age = int(input())
if my_age > 24:

Method 3

print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if int(myAge) > 24:
     print('You are old, ' + myName)
else:
     print('You will be old before you know it.')

Just a small thing about your code. You should convert the input from myAge to an integer (int) (number) and then compare that number to the number 24.;

Also, you should usually not add strings together as it is consider non-pythonic and it slow. Try something like print ('Hello, %s' % myName) instead of print ('Hello, ' + myName).

Python Strings Tutorial

Method 4

Use int(myAge). I always use raw_input and also, you dont have to print your questions. Instead put the question in with your raw_inputs like so:

myName = raw_input("Whats your name?")
print ('Hello, ' + myName)
myAge = raw_input('How old are you?, ' + myName)
if int(myAge) > ('24'):
    print('You are old, ' + myName)
else:
    print('You will be old before you know it.')


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x