1

I basically have created a program in python to get the largest number by accepting two numbers. This works fine. But if you try to enter 12 as first number and 4 as a second number, then I don't know what kind of logic error is going on!!

Code:

num1 = input("Enter a number: ")
num2 = input("Enter one more number: ")

print(num1,num2)

if num1 > num2:
    print(num1, "is greater than", num2)
elif num2 > num1:
    print(num2, "is greater than", num1)
else:
    print("Both the numbers are equal!")

Rishabh Ankit
  • 247
  • 2
  • 11

2 Answers2

3

You are comparing strings, as input() returns str

Change the type to int then compare

num1 = int(input("Enter a number: "))
num2 = int(input("Enter one more number: "))

print(num1,num2)

if num1 > num2:
    print(num1, "is greater than", num2)
elif num2 > num1:
    print(num2, "is greater than", num1)
else:
    print("Both the numbers are equal!")

what you are doing

for eg: if numbers are 321 and 1234

It won't give an error. But "321">"1234" will be considered as Truewhich is in-correct

Kuldeep Singh Sidhu
  • 3,748
  • 2
  • 12
  • 22
1

Python takes input as String

Convert your String input into Integer

Just make these 2 changes in starting 2 lines :

num1 = int(input("Enter a number: "))
num2 = int(input("Enter one more number: ")) 
Rishabh Ankit
  • 247
  • 2
  • 11