Matt Johnson
Matt Johnson

Reputation: 55

Comparing a variable to an `int` is not working

My program needs an integer from the user, so I'm trying to create a loop that will occur if they enter a non-integer and doesn't end until they enter an integer. I've tried:

PlayerCount = input("How many players?")
while PlayerCount != int:
    try:
        PlayerCount = int(PlayerCount)
    except ValueError:
        print("Please enter a number between 3 and 5")
        PlayerCount = input("How many players?")

However, when a valid input is entered the loop doesn't continue, or end and allow the rest of the program to run. I simply see a line break in IDLE and a blinking cursor. Is there something else I need to do to properly end the while loop? I expected the loop to end automatically once the try block succeeds.

Upvotes: 3

Views: 1295

Answers (6)

pandakkkk
pandakkkk

Reputation: 1

PlayerCount = input("How many players?")
while type(PlayerCount) != int:
    try:
        PlayerCount = int(PlayerCount)
    except ValueError:
        print("Please enter a number between 3 and 5")
        PlayerCount = input("How many players?")
    else:
        pass

else part will be executed when there is no any exception.

Upvotes: 0

Hai Vu
Hai Vu

Reputation: 40688

Ask Jacob pointed out, comparing to int is incorrect. I suggest to have a loop which will retry until the input in an integer between 3 and 5:

while True:
    player_count = input('How many players (3-5)? ')
    try:
        player_count = int(player_count)
        if player_count in (3, 4, 5):
            break
    except ValueError:
        pass

A few points:

  • This approach includes only 1 input function call
  • The user input is also converted to int
  • It checks for both condition: an int and the range 3..5

Upvotes: 0

Eb946207
Eb946207

Reputation: 778

Do this:

PlayerCount = input("How many players?")
while True: #loop forever until `break`
    try:
        PlayerCount = int(PlayerCount)
    except ValueError:
        print("Please enter a number between 3 and 5")
        PlayerCount = input("How many players?")
    else: #no error
        break #exit loop

else runs when not error is made in try.

Upvotes: 4

U13-Forward
U13-Forward

Reputation: 71560

Try using isinstance in the while loop on the first line, then there is no need to modify the inside of the while loop:

PlayerCount = input("How many players?")
while not isinstance(PlayerCount, int):
    try:
        PlayerCount = int(PlayerCount)
    except ValueError:
        print("Please enter a number between 3 and 5")
        PlayerCount = input("How many players?")

Upvotes: 2

Picachieu
Picachieu

Reputation: 3782

In your code, you are comparing the user input to the int class. You aren't checking if it's an instance of int. And anyway, you're already checking with your try-except block. The except block executes when the input is not a valid integer.

Just change it to this:

PlayerCount = input("How many players?")
while True:
    try:
        PlayerCount = int(PlayerCount)
        break
    except ValueError:
        print("Please enter a number between 3 and 5")
        PlayerCount = input("How many players?")

As mentioned in the comments, an alternate way would be to use isinstance(PlayerCount, int), which returns a bool value based on if PlayerCount is an instance of int.

Upvotes: 0

jfhr
jfhr

Reputation: 1123

The error is in the line while PlayerCount != int:.

PlayerCount != int will always be true. What you probably want to do is check if PlayerCount is of an integer type. But you're actually checking if it is equivalent to the class int itself. Note that an actual integer is different from the int class.

To check whether PlayerCount is an instance of the int class, replace the condition with while not isinstance(PlayerCount, int):.

Upvotes: 3

Related Questions