rkshraddha
rkshraddha

Reputation: 11

Else statement not executed with string or float input

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

if (str(num1).isdigit()) and (str(num2).isdigit()):
    if num1>num2:
        print(num2, num1)
    else:
        print(num1, num2)
else:
    print("That is not an integer!")

Error that I am getting:

Traceback (most recent call last):

File "C:\Users\shrad.spyder-py3\temp.py", line 183, in num2 = int(input("Enter the second integer: "))

ValueError: invalid literal for int() with base 10: '2.2'

Upvotes: 0

Views: 281

Answers (4)

Hadrian
Hadrian

Reputation: 927

int() will only accept an integer - that is a number without a decimal point which is why it won't accept 2.2. You probably want to use float().

Upvotes: 1

Mudragreas
Mudragreas

Reputation: 39

Well, you know what they say, 'never trust the user input'. Even if the user is, well, yourself.

The statement: input("Enter the first integer: ") returns a string (the one you typed), which, in turn, passed to int() is attempted to be converted to an integer.

It is a requirement of int function, when given a string as input, that this string is strictly a depiction of an integer (and not a floating point number, like, say, 2.2)

You can experiment with the very first line: num1 = int(input("Enter the first integer: ")) then print(num1) to figure out how this works for yourself; when it raises and when not.

The problem in the code you posted is that you first try to convert to an int, and later check if the input was actually an int. So, Python interpreter had to face the problem first and used it's own, automatic way to report it: an exception.

A way to come around this could be to postpone the string -> int coversion until it is actually needed. Luckily, that's only one place, in the comparison, which would then become: if int(num1) > int(num2): . That would spare you the trouble from the int() conversions in the beginning, plus the back-to-string conversions for int-checking ( if (str(num1).isdigit()) and (str(num2).isdigit()): could just become if num1.isdigit() and num2.isdigit(): since num1, num2 have not been coverted yet. )

Alternatively, if you want decimal point numbers to be converted without errors, you can use the float() function (which can conveniently convert an int as well).

You can also try to use exception handling, such as:

try:
    num1 = int(input("Enter the first integer: "))
    num2 = int(input("Enter the second integer: "))
except Exception as e:
    print("Error converting input to integers", e)

which would in turn, spare you from the need of validating the input below...

Upvotes: 0

alani
alani

Reputation: 13079

You are trying to convert to an int before you have tested whether it consists of digits. Wait until you have done this test before doing the conversion.

num1 = input("Enter the first integer: ")  # keep it as a string for now
num2 = input("Enter the second integer: ")

if num1.isdigit() and num2.isdigit():
    num1 = int(num1)  # NOW convert to int
    num2 = int(num2)
    if num1>num2:
        print(num2, num1)
    else:
        print(num1, num2)
else:
    print("That is not an integer!")

By the way, be aware that your isdigit test will not work for negative integers.

Upvotes: 1

2.2 is a float, not an int, but you're trying to cast it to an int.

Upvotes: 0

Related Questions