0
# Program1: Take two numbers as input from User and print which one is greater or are they equal.

num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
if num1 is num2:
    print("The two numbers are equal")
elif num1 < num2:
    print("The first number is less than the second number")
else:
    print("The first number is greater than the second number")


Enter the first number: -10 Enter the second number: -20 The first number is less than the second number

Process finished with exit code 0

What is wrong in above code to compare two numbers? How come python missing basic -ve number comparison?

2
  • Does this answer your question? When, if ever, to use the 'is' keyword in Python? Commented Feb 21, 2024 at 9:30
  • 1
    Also, your inputs are string values - comparing them like num1 < num2 compares the strings lexicographically (basically, alphabetically). If you want to compare them numerically, you'll need to parse those strings into numbers. Commented Feb 21, 2024 at 9:31

3 Answers 3

1

First, input() returns string. SO you need to cast the input values to integer by using int().

Second, when you use if num1 is num2:, the is operator(keyword) is testing if num1and num2refer to the same object or not.

Third, Enter the first number: -10 Enter the second number: -20 The first number is less than the second number

Process finished with exit code 0

You know that -10 and -20 are numbers, but as mentioned in First Para above, input()is returning string values, python is doing lexicographical comparison, and ascii of - is highter than digit.

So, your code should be like this instead

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

if num1 == num2:
    print("The two numbers are equal")
elif num1 < num2:
    print("The first number is less than the second number")
else:
    print("The first number is greater than the second number")
Sign up to request clarification or add additional context in comments.

Comments

0

In your case you should use '==' statement instead of 'is' statement.

'==' actually compare the value where 'is' check if they are same object.

See a previous discussion about that

2 Comments

Since what he tries to do is to not only check if they are the same value but also to check what value is higher/lower the best way to do it is to parse the inputs to int values and then do those comparisons instead of just using the '==' statement.
Since what he tries to do is to not only check if they are the same value but also to check what value is higher/lower the best way to do it is to parse the inputs to int values and then do those comparisons instead of just using the '==' statement.
0

You are doing a string comparison, not a numerical comparison. The string "-10" does come before "-20" since "1" comes before "2" in lexicographical order. To do an integer comparison you should cast the values you receive to int:

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.