0
a=input()
b=input()
if a<b :
print('B is maximum',b)
else:
print('A is maximum',a)

for this program if we give a=10 b=2 it will give B is maximum why ?

2 Answers2

4

It sounds like you're using Python 3.x. In this version, input() returns strings, not numbers, so it's doing a lexicographic comparison, not a numeric comparison. If you want to compare integers, you have to convert the input first:

a = int(input())
b = int(input())
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Note that this is only in Python 3. The question didn't specify a Python version, so in the unlikely event that OP is actually using Python 2, then something much stranger is happening. – seaotternerd Mar 03 '15 at 04:40
  • In python 2.x I think using `input` will return the number as expected. Am I wrong? – Thiyagu Mar 03 '15 at 04:41
  • That's correct. python 3.x `input` is like python 2.x `raw_input`. – Barmar Mar 03 '15 at 04:43
  • In either case (Python 2 or 3), this will work if everything else is all right. However, the system will crash if the user enters something wrong. Also, this method is better than Python 2's input because it does not evaluate the given value such as a function on variable name. – jkd Mar 03 '15 at 05:07
0

To be precise, it is because you are using Python 3.x. In Python 2.x, there were two variants, raw_input() which returns a string and input() which returns an evaluated result. In Python 3.x, raw_input() was renamed to input(), and the original input() function was dropped (though you can easily get the behaviour by wrapping it over an eval function).

Strings are compared lexicographically so '10' < '2' though if they had been numbers the result would had been antagonistic.

Abhijit
  • 62,056
  • 18
  • 131
  • 204