0

I wrote a little programm in python which has to give the biggest nummber of 2 inputs. The code is following:

a = input("insert a")
b = input("insert b")
if a < b:
    print(b)
else:
    print(a)

the problem is, this Code works with some numbers but not with all. Foe example if i insert a=5 and b=10 or b=5 and a=10 it would always give me 5 back, even 10 is bigger than 5. I dont know if the problem is from my Code or its a bug in Pycharm beacaue i tried it in Pycharm in visual Studi Code and it gave me the same result and i dont understand why. If somone can explain this to me, i would be very grateful

sari
  • 48
  • 3

2 Answers2

3

the issue is cause by input() returning a string not a number, this causes the evaluation to compare alphabetically not numerically. you can cast the variables to int like so

a = int(input("insert a"))
b = int(input("insert b"))
if a < b:
    print(b)
else:
    print(a)
Patrick
  • 1,189
  • 5
  • 11
  • 19
2

Since Python 3, input() returns a string which you have to explicitly convert to ints, with int, like this

x = int(input("Enter a number: "))
y = int(input("Enter a number: "))

For values that can have a fractional component, the type would be float rather than int:

x = float(input("Enter a number:"))
Tal Folkman
  • 2,368
  • 1
  • 7
  • 21