Home > Net >  Python 3.10 - Issue with detecting variable type (begginer)
Python 3.10 - Issue with detecting variable type (begginer)

Time:05-26

Good afternoon everyone.

First of all I am a total beginer in coding and Python itself. To be honest I am not even sure is that a proper place to ask such a 'newbie' question.

But to the point.

During the online course I was asked to write a simple program to guess numbers. I did that and the code works quite ok: https://pastebin.com/XwZ2qcab

Although I wanted to improve the code to allow user to type a non int variable and not crash. I have used ' if type(userNumber) != int:' in the upgraded version of the code: https://pastebin.com/JQarjjSw but it does not work.

The issue I have is (I think) here:

for i in range(1, 7):
userNumber = input('Take a guess')
if type(userNumber) != int: #This line is my main issue, if I delete it code works like a charm.
    break
elif int(userNumber) > int(number):
    print('No! Your number is too high. Try again')
elif int(userNumber) < int(number):
    print('No! Your number is too low. Try again')
else:
    break

I have no idea why the line if type(userNumber) != int: break is not executed and pyCharm goes directly to: elif int(userNumber) > int(number): and crashes.

Funny thing is that on pythontutor.com code works as intended. Checks for userNumber and if it is not an int breakes the IF loop.

Hope that is somehow clear.

CodePudding user response:

You get your userNumber variable from input function. And it always returns a string. So you have a few options you can use to check if user gives a digits.

First option is to use str.isdigit() method

userNumber = input('Take a guess')
if not userNunber.isdigit():
    # user entered something else than digits
    break

Another option could be using try-except method:

userNumber = input('Take a guess')
try:
    # try to convert user input to integer
    userNumber = int(userNumber)
except ValueError:
    # We failed so user entered invalid value
    break

CodePudding user response:

In this case, you need to use a try… except… statement:

try:
    int(“12ab”)
except ValueError:
    print(“Not a number”)

Your full code would be:


for i in range(1, 7):
    userNumber = input('Take a guess')
    try:
        num = int(userNumber)
    except ValueError:
        print(“Not a number”)
        break

    if num > int(number):
        print('No! Your number is too high. Try again')
    elif num < int(number):
        print('No! Your number is too low. Try again')
    else:
        break

You can read more about it here.

CodePudding user response:

Extending on ex4's answer, you should use .isdigit() to check if the input is numeric, however where this is a game I assume you don't just want the game to end using a break when the player enters a bad input and you'd rather have them guess again.

Try the following segment. It takes inputs until the player enters an input which isn't a digit, where it gets the player to enter another input until it is a digit.

for i in range(1, 7):
    userNumber = input('Take a guess')
    while not userNumber.isdigit():
        userNumber = input('What you have entered is not a number, please guess again')
    if int(userNumber) > int(number):
        print('No! Your number is too high. Try again')
    elif int(userNumber) < int(number):
        print('No! Your number is too low. Try again')
    else:
        break
  • Related