In the following Python solution 3, when using .isdigit() to determine that the input value is -5, why can it still be determined to be int? (According to the definition, negative numbers and floating point numbers should be false)

Program purpose: determine whether the input value is an integer or a floating point number or a string

I wrote 3 solutions myself, but in the third solution, I used chatGBT to check, and it kept reminding me that substituting “-5” would be an error in judging it as float, but after I executed it myself, the result was always judged to be int, which is correct.??

(Environment IDE Spyder+ Python 3.12.4)
Please kindly give me some advice

#Johnny solution 1
def johnny(value):
try:
x=float(value)
if x.is_integer():
return “int”
else:
return “float”
except ValueError:
return “str”

value=input(“Please input :”)
print(johnny(value))

#Johnny solution 2
def johnnysuper(value):
try:
int(value)
return “int”
except ValueError:
pass
try:
float(value)
return “float”
except ValueError:
return “str”

value=input(“Please input :”)
print(johnnysuper(value))

#Johnny solution 3
def johnnysuper(value):
try:
value.isdigit()==True
return “int”
except ValueError:
pass
try:
float(value)
return “float”
except ValueError:
return “str”

value=input(“Please input :”)
print(johnnysuper(value))

ChatGPT doesn’t know Python. It’s just really good at talking.

Your code doesn’t do what you think it does. Try a few other examples on your third function and see what they do. Can you find a pattern? Try these inputs:

  • 1
  • 10
  • -1
  • 1.5
  • a
  • q
  • spam
  • spam1
  • 1spam

Do you notice how consistent your code is? Maybe that’s a clue.

1 Like

Thanks for your clues 。
From you input ,I finally realize the bug is that the false still trigger next step “return int”

    value.isdigit()==True
    return "int"

According to this ,I modify the solution 3 as below and i think it is workable now
Please feel free to commment if any valuable input ,thanks Chris!

def johnnysuper(value):
try:
if value.isdigit():
return “int”
elif int(value)<0:
return “int”
else:
pass
except ValueError:
pass
try:
float(value)
return “float”
except ValueError:
return “str”

value=input(“Please input :”)
print(johnnysuper(value))

First – in this forum, you want to make your code look like code, you do that by putting itside “fences” (or click the </> button in the UI: That will preserve theindentation which is critical – and make it look better …

Here’s your code, with my review:

def johnnysuper(value):
    try:
        if value.isdigit():
            return “int”
        elif int(value)<0:
            return “int”
        else:
                pass
    except ValueError:
            pass
    try:
        float(value)
        return “float”
    except ValueError:
        return “str”

value=input(“Please input :”)
print(johnnysuper(value))

First, the question is a bit unclear – input() always returns a string.

So what you want: is the input string a valid integer, or a valid float or another non-numeric string.

Second – there are essentially two ways to determine if a string contains an number:

  1. examine the string, loook for valid characters (e.g. isdigit())
  • fairly easy for integers, more complex for floats
  1. use the machinery built in to Python: try to make it a number, and see if that works.

You have actually done both of these in your code, but you only need to do one – you’ve realized that the minus sign (hyphen) is not a digit, so that has to be done separately anyway – in that case, you are using the "try to turn it into an int"approach – great – but then there’s no need for the isdigit call at all.

You’re close – but make it a simpler and more consistent (which is pretty much simply removing some code :frowning: and you’ll have it.

Have fun!

1 Like

Thanks for your comment ,Chris ! Appricated for two Chris !

Just to pinpoint str.isdigit() expected behaviour:

/…/ Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10 /…/

This means that not all strings which pass .isdigit are convertible to int:

>>> "123".isdigit()
True
>>> "1²".isdigit()
True
>>> int("1²")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1²'
1 Like