def listMaker(a, b):
if b.isdigit() == False:
print("Sorry is not a valid input")
else:
newList = [a] * b
return newList
I am getting the error:
AttributeError: 'int' object has no attribute 'isdigit'
How would I fix this?
isdigit when you want to verify that each and every character in a string is a single digit, that is, not punctuation, not a letter, and not negative.
The isdigit() returns False if the string contains these characters. To check whether a character is a numeric character or not, you can use isnumeric() method.
isdigit() only returns true for strings (here consisting of just one character each) contains only digits. Because only digits are passed through, int() always works, it is never called on a letter.
Definition and Usage. The isdigit() method returns True if all the characters are digits, otherwise False. Exponents, like ², are also considered to be a digit.
isdigit()
is a method of a str
class. Depending on what are trying to achieve, either convert b to string first:
if not str(b).isdigit()
or (and better), use isinstance
to check attribute types:
if not isinstance(b, int)
Seems that you already have integer, if don't sure you can do:
if isinstance(b, int):
print("Sorry is not a valid input")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With