Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm not sure how to fix this while loop to give "invalid entry" for when the input is not 8 or 9 but proceeed normally or break when the valid answer is given


x = ""

while True:             # Loop continuously
    game_type = int(input("Enter game type, either 8-ball or 9-ball with a single number (aka = 8 or 9): "))   # Get the input
    if game_type != "8" and game_type != "9":
      print(x)
      print("Invalid game type, please enter 8 or 9")
      print(x)
    elif game_type == 8 or game_type == 9:       # If it is a blank line...
      break 

print("the game type is",game_type)


What I have tried:

I've tried a variety of while formats but cannot seem to translate it successully into this program
Posted
Updated 10-Feb-20 22:18pm
Comments
Richard MacCutchan 11-Feb-20 4:15am    
You are converting the input to an integer but then you compare it to a string.

Python
while True:             # Loop continuously
    game_type = int(input("Enter game type, either 8-ball or 9-ball with a single number (aka = 8 or 9): "))   # Get the input
    if game_type != 8 and game_type != 9:
      print(x)
      print("Invalid game type:", game_type, "please enter 8 or 9")
      print(x)
    else:
      break 

print("the game type is",game_type)
 
Share this answer
 
Quote:
I'm not sure how to fix this while loop to give "invalid entry" for when the input is not 8 or 9 but proceeed normally or break when the valid answer is given

You need to make your mind and choose, input is string or number, not both.
Python
x = ""

while True:             # Loop continuously
    game_type = int(input("Enter game type, either 8-ball or 9-ball with a single number (aka = 8 or 9): "))   # Get the input
    if game_type != "8" and game_type != "9":
      print(x)
      print("Invalid game type, please enter 8 or 9")
      print(x)
    elif game_type == 8 or game_type == 9:       # If it is a blank line...
      break 

print("the game type is",game_type)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900