Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isdigit not working for me

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?

like image 538
akakaa Avatar asked Sep 27 '22 09:09

akakaa


People also ask

Does Isdigit work for negative numbers?

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.

How do you know if its Isdigit?

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.

Does Isdigit work for strings?

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.

How does Isdigit work in Python?

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.


2 Answers

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)
like image 89
anti1869 Avatar answered Sep 30 '22 09:09

anti1869


Seems that you already have integer, if don't sure you can do:

if isinstance(b, int):
    print("Sorry is not a valid input")
like image 22
Eugene Soldatov Avatar answered Sep 30 '22 07:09

Eugene Soldatov