Random Guy
Random Guy

Reputation: 81

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 (a-z) 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.

Upvotes: 0

Views: 632

Answers (4)

TCFP
TCFP

Reputation: 185

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.

Upvotes: 1

Davo
Davo

Reputation: 566

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():

Upvotes: 1

Marc Sances
Marc Sances

Reputation: 2614

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.

Upvotes: 1

John Gordon
John Gordon

Reputation: 33335

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.

Upvotes: 4

Related Questions