-1

I am trying to find of biggest of 3 numbers using python3.6. But its returning wrong value.

#!/usr/bin/python3

a=input("Enter first number: ")
b=input("Enter second number: ")
c=input("Enter Third number: ")
if (a >= b) and (a >= c):
    largest=a
elif (b >= a) and (b >= c):
    largest=b
else:
    largest=c

print(largest, "is the greatest of all")

If I provide, a=15; b=10 and c=9 Expected output should be 15.

But I am getting actual output as 9.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Navi
  • 67
  • 6
  • 4
    You are comparing strings, not numbers. If you convert them to numbers, they will be compared as numbers. – khelwood May 09 '19 at 08:24
  • Possible duplicate of [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – DavidG May 09 '19 at 08:32

4 Answers4

1

You can use the max() builtin function of python: https://www.freecodecamp.org/forum/t/python-max-function/19205

Kraego
  • 2,978
  • 2
  • 22
  • 34
1

input() returns strings. You need to convert the strings to class int to compare their numeric value:

a = int(input("Enter the first number: "))

In string comparisons, 9 is greater than 1. You can try in the REPL:

>>> '9' > '111111111111111111'
True
0

Like the comment of khelwood mentioned it You are comparing strings, not numbers. – khelwood. You should first cast the input values to integers (or float) and then compare them.

More about casting in python can be found here

desmaxi
  • 293
  • 1
  • 13
0

As khelwood pointed out in comment those inputs are used as strings.

try inserting this after input and its fixed:

a = int(a)
b = int(b)
c = int(c)
Nuzvee
  • 66
  • 1
  • 6