1

I want to ask the user to input a value and then print its length, if the input is a string. If the user's input is an integer or a decimal, I want to print "Sorry, integers don't have length" or "Sorry, floats don't have length", respectively.

I'm making use of exception while trying to convert the input into float or integer.

Here is my code:

c=input("enter a string: ")

def length(c):
    return len(c)

try:
    float(c)
    if float(c)==int(c):
        print("Sorry integers don't have length")
    else:
        print("Sorry floats don't have length")
except:
    print(length(c))

The output results are as follows:

> enter a string: sfkelkrelte
11
> enter a string: 21
Sorry integers don't have length
> enter a string: 21.1
4

When I input an integer, the error message is displayed correctly, as the conversion float() is possible. But, in case of a floating point number, the interpreter goes to except block, though it should have executed the try block.

Why does this happen?

pault
  • 41,343
  • 15
  • 107
  • 149
laxman prasad
  • 21
  • 1
  • 4

3 Answers3

1

Also, why not apply the EAFP principle (What is the EAFP principle in Python?) to the second condition, too?

s = input("Input: ")
try:
    float(s)
    try:
        int(s)
        print("Sorry, you have entered an integer.")
    except:
        print("Sorry, you have entered a float.")
except:
    print(len(s))

I have omitted to check for ValueError as the exception, because you didn't check that in your code, too. However, you should have a look at How to properly ignore exceptions

finefoot
  • 9,914
  • 7
  • 59
  • 102
  • your code is working fine. however i dont get the logic of inner exception block. for eg, if the input is "21.0", then int(21.0) will not raise an exception. so why is it still executing the except block, and not the try block? – laxman prasad May 31 '19 at 14:05
0

The part that is raising the exception is int(c):

c = "21.1"
print(int(c))
# ValueError: invalid literal for int() with base 10: '21.1'

One minor change would fix this for you:

c="21.1"

try:
    float(c)
    if "." not in c: #<---- check if "." is in the string c AFTER conversion to float
        print("Sorry integers don't have length")
    else:
        print("Sorry floats don't have length")
except:
    print(len(c))
# Sorry floats don't have length

However, it's generally bad practice to blindly catch all exceptions. This would have the unintended side effect of any error in your program triggering the except block.

It would be more appropriate to catch only the one you are expecting.

c="21.aakdjs1"

try:
    float(c)
    if "." not in c:
        print("Sorry integers don't have length")
    else:
        print("Sorry floats don't have length")
except ValueError:
    print(len(c))
# 10

For future debugging, you can always print the exception.

pault
  • 41,343
  • 15
  • 107
  • 149
  • I still don't get the `if float(c)==int(c):` business. If they're going to reject floats and ints anyway, just test for `float`? – roganjosh May 30 '19 at 18:42
  • 2
    Seems like they want different print statements for `int`s vs. `float`s. Probably homework. – pault May 30 '19 at 18:43
  • in your code, if the input given is "21.0", the line "Sorry integers don't have length" is being displayed. – laxman prasad May 31 '19 at 14:03
  • yes because `float("21.0") == int(21.0)`. I've made an update @laxmanprasad – pault May 31 '19 at 14:48
0

Input results to a string format so convert the string into float Then check if it is an integer. Change your code into:

old: if float(c) ==int(c):

new: if c.isdigit():

UPDATED:

enter a string: 21.0
Sorry floats don't have length
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38
  • if the input is 21.0, "float(21.0) ==int(float(21.0))" will evaluate to true. hence decimal numbers with the fractional part being 0, will be considered as integers, according to your code. – laxman prasad May 31 '19 at 14:09
  • i updated my answer. the try except block will catch if float or not. Then isdigit() will catch if it is an integer. – jose_bacoy May 31 '19 at 14:42