Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

Below is my code:

def ercheck (var):

    nums = [1,2,3,4,5,6,7,8,9,0]

    error = False

    for num in nums:

        if str(num) not in (var):

            error = True

    for char in str(var):

        if char not in (var):

            error = True

    if error == True:

        print("Invalid argument(s), Try again.")

        quit()

It always gives me an error message, that I coded in. After some testing, it seems like my for loop num in nums: if str(num) not in (var): error = True is the problem.

1 Answer

0 votes
by (36.8k points)

To check if input string is a number then you can use .isnumeric().

def ercheck (var):

    if not str(var).isnumeric():

        print("Invalid argument(s), Try again.")

ercheck("500")

ercheck("11!1")

# Invalid argument(s), Try again.

Do check out Python Data Science Course which helps you understand from scratch 

Browse Categories

...