简体   繁体   中英

My program doesn't recognize .isdigit as True

In my program I want to check if the stroke symbol is a common digit (0-9)

.isnumeral works strange because it counts alphabeticals (az) as True, well then I lurked in and got that .isnumeral isn't actually searching exclusively for what I want - digits. And through the manual I found .isdigit but:

dna = 'a3'
start = 0
end = 1
if dna[end].isdigit is True:
    print('Yes')

It's not working and 'Yes' isn't showing as expected.

if dna[end].isdigit is True:

isdigit() is a function, not an attribute.

You forgot the parentheses on the end, therefore you're referring to the function object itself , instead of the result of calling the function.

You must actually call the isdigit() method:

dna = 'a3'
start = 0
end = 1
if dna[end].isdigit():
    print('Yes')

This gives your expected answer, True.

If you do dna[end].isdigit it just gives an object <built-in method isdigit of str object at address> which won't evaluate.

dna[end].isdigit in this case is referring to a str.isdigit function. If you do print(type(dna[end].isdigit)) you will see what I mean.

To call the function instead, add paranthesis like this if dna[end].isdigit():

Two things:

  • there's no need to compare to true, just use the result from isdigit()

  • isdigit() is a function, which is truthy on its own, but does not equate to True

Check out the Python docs for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM