0

I'm trying to make a simple quiz for a project, but I can't get an integer checker to work.

I'm trying to make it so it prints an error for floats and strings, then lets you enter another thing for the same question. My previous one gave a python error and ended the program when entering a float / string.

def int_checker(question):
    user = input(question)

    passcheck = False
    while passcheck is False:
        if isinstance(user, int) is True:
            passcheck = True
            return passcheck
    
    
        elif isinstance(user, int) is True:
            print()
            print("Whole numbers only, please.")
            passcheck = False
            return passcheck
    
        else:
            print()
            print("Numbers only, please.")
            passcheck = False
            return passcheck
    return user

When I type anything, it gives me the final print statement, then lets me type another input in.

So, is there anyway I can make it say the correct message for each input type, and have it so if I type in an integer, it lets the code use that input?

Thanks in advance.

max wiklund
  • 103
  • 1
  • 5
  • Side-note: [Testing `if expression is True:` is almost always bad](https://www.python.org/dev/peps/pep-0008/#programming-recommendations). There are *incredibly* rare cases where being actually `True`, as opposed to merely being "truthy" is important, but 99.99% of the time (if not more) you should just be doing `if expression:`. `isinstance` checks are definitely one of those times (they already return `True` or `False`, and checking if they're actually `True` is pointless overhead). – ShadowRanger Oct 27 '20 at 17:37
  • `input` **always** returns a string so checking if it's an int is useless... – Tomerikoo Oct 27 '20 at 17:57

1 Answers1

1

Since you are using Python 3, user variable will always be a string object, because input function in Python 3 always returns string, so using isinstance function won't help. That's why interpreter won't ever execute if and elif block. If you need to check if user input is a number, then you should implement something like this:

def isValidInt(stringInput):
    try:
        int(stringInput)
        return True
    except ValueError:
        return False

and use isValidInt(user) instead of isinstance(user, int).

Few things I want to point out:

  1. I noticed that condition in both if and elif are same. Probably, you wanted to do something else and wrote it by mistake.
  2. Having while loop in your function won't make any difference. If you drop it, it will work same.

Refer this implementation if you face any problems:

def intChecker(question):
    while True:
        userInput = input(question)

        if isValidInt(userInput):
            return int(userInput)

        if isValidFloat(userInput):                 # Refer isValidInt for implementation of isValidFloat
            print("Whole numbers only, please.")
        else:
            print("Numbers only, please.")
Aniket Tiratkar
  • 798
  • 6
  • 16