Never Ending Validation Check in Python - python

Working on a battleship game in python. My function to check the user's 'guess' input in resulting in an endless validation loop. I want a guess in the format of 'a10' on a 10x10 grid. The validation function i have built to help validate is as follows:
def validate_guess(self,guess):
while True:
if (len(guess)) in range(2, 4):
if guess[0] not in 'abcdefghij' or guess[1:] not in '1,2,3,4,5,6,7,8,9,10':
print("Sorry the Grid is a 10x10 square you must enter a valid position. Try again...")
continue
else:
return guess
else:
if len(guess) < 2 or len(guess) > 3:
print("Oops! That's too not the right amount of characters. Please try again...")
continue
If the user guesses an incorrect value--it does spot it--but the error is returning a never ending loop with the printed error statement.
This is the portion of my game where the validation function is being used:
while True:
print("\n")
# get guess from player one in coordinate form (example: a10)
guess = input("{}'s turn to guess: ".format(player1.player))
# strip characters from the guess input
guess = guess.strip()
# validate the guess
guess = self.validate_guess(guess)
# append the guess to a list for player 1
player1.guesses.append(guess)
# break down the coordinates into x and y variables
x, y = ship1.split_guess(guess)
# increment guesses
guesses += 1
# loop to assess whether, hit, miss or a won game
if any(guess in ship for ship in grid2.play_two_board):
print("HIT")
grid2.print_guess_board(x, y, "h")
for ship in grid2.play_two_board:
try:
ship.remove(guess)
print(len(Board.play_two_board))
self.check_if_sunk(ship)
win = self.play_one_win_check(player1.player)
if win == 1:
print ("GAVE OVER!")
break
except ValueError:
pass
else:
print("Miss!")
grid2.print_guess_board(x, y, "m")
Not sure if it might be because i have two While statements? Just really stuck here would appreciate any guidance. Thanks.
*****************************8
edit --changed function to include the guess without actually passing it that value from the input statement but still getting same problem.
def validate_guess(self):
guess = input("Please enter a location:")
while True:
if len(guess) in range(2, 4):
if guess[0] not in 'abcdefghij' or guess[1:] not in '1,2,3,4,5,6,7,8,9,10':
print("Sorry the Grid is a 10x10 square you must enter a valid position. Try again...")
continue
else:
return guess
else:
if len(guess) < 2 or len(guess) > 3:
print("Oops! That's too not the right amount of characters. Please try again...")
continue
and just calling it like this:
while True:
print("\n")
# get guess from player one in coordinate form (example: a10)
# guess = input("{}'s turn to guess: ".format(player1.player))
# strip characters from the guess input
# guess = guess.strip()
# validate the guess
guess = self.validate_guess()

Within validate_guess, when the user enters a bad value, you have to get new input before you check again. See here.
Either return an error code (True/False ?) from your function, or have the function loop until the input is valid. Just add a command for new input at the bottom of the function's while loop.

Related

While loop keeps printing constantly, doesn't allow for user input

I'm trying to complete this assignment asking user for a number and if it's not -1 then it should loop. if it's -1 then to calculate the average of the other numbers.
I'm getting stuck with the actual loop - it endlessly keeps printing the message to user to enter a different number - as in the picture - and doesn't give user a chance to enter a different number. Please help, I've been through so many videos and blogs and can't figure out what's actually wrong.
#creating a list for later calculations:
wrong = []
#asking for input:
input("Hi, We're gonna play a guessing game. When asked enter a number between -10 and 10.\nIf not correct you'll have to guess again ^-^")
num =int(input("number:"))
#looping
while num != -abs(1):
wrong.append(num)
print("Nope, guess again:")
if num == -abs(1):
break
av = sum(wrong) / len(wrong)
print ("You got it! The average of your wrong answers is: ")
print(av)
print("The End")
print("Nope, guess again:")
You need to include the input function in the loop if you want it to work. I corrected the rest of your code as well, you don't need the if condition. More generally you should avoid to use break, it often means you are doing something wrong with your loop condition. Here it is redondant and the code after break is never executed.
wrong = []
input("Hi, We're gonna play a guessing game. When asked enter a number between -10 and 10.\nIf not correct you'll have to guess again ^-^")
num = int(input("Number: "))
while num != -1 :
wrong.append(num)
num = int(input("Nope, guess again: "))
av = sum(wrong) / len(wrong)
print(f"You got it! The average of your wrong answers is: {av}\nThe End")
You are just breaking the loop before printing the results, first print the results, then break the loop.
And a while loop isn't necessary for your program, use if condition wrapped in a function instead:
#creating a list for later calculations:
wrong = []
#asking for input:
input("Hi, We're gonna play a guessing game. When asked enter a number between -10 and 10.\nIf not correct you'll have to guess again ^-^")
#looping
def go():
num =int(input("number:"))
if num != -abs(1):
wrong.append(num)
print("Nope, guess again:")
if num == -abs(1):
av = sum(wrong) / len(wrong)
print ("You got it! The average of your wrong answers is: ")
print(av)
print("The End")
break
print("Nope, guess again:")
go()
There are lots of issues in the code.
If you want to get inputs in while looping, you should include getting input code inside the while loop like below,
while num != -1:
......
num =int(input("number:"))
......
Also you don't have to include 'break' inside the while loop because, when num != 1, the loop will stop.
You should ask for input inside your loop, but you just print "Nope, guess again:".
wrong = []
print("Hi, We're gonna play a guessing game. When asked enter a number between -10 and 10.\n"
"If not correct you'll have to guess again ^-^")
num = int(input("number: "))
# looping
while num != -1:
wrong.append(num)
num = int(input("Nope, guess again: "))
av = sum(wrong) / len(wrong)
print(f"You got it! The average of your wrong answers is: {av}\nThe End")

How to end a try loop in a dice game at the correct place?

I am trying to make a program where the player enters a number (1-6) to guess what a fair randomised dice lands on.
Then it should stimulate the dice throw, and informs the player whether it was correct or not.
It should continue to ask the player to guess by entering a number until the guess is correct.
If it is correct the game should end.
If it is not correct the game continues to ask the player to select a number.
I have tried:
from random import randint
def validateNumber():
valid = False
if 1 <= number_in <=6:
valid = True
return valid
game_1 = randint(1,6)
while True:
try:
number_in = int(input("Enter a number between 1-6: "))
is_valid = validateNumber()
if not is_valid:
number_in = int(input("Enter a number between 1-6: "))
if number_in == game_1:
print(f"The result was {game_1}. Your guess was correct!")
else:
print(f"The result was {game_1}. Your guess was NOT correct. Please try again")
except ValueError:
break
However, now it does not end when the guess is correct.
Does anyone know how to fix this?
I have tried many variants, but this is the closest I got to the wanted result, but still it is not satisfactory. Also it does not seem to truly handle the user input?
(I am quite new to Python and trying to learn the most before starting college in January:)) All help appreciated!
Your main issue is that you should break out of the while loop when the user guess the number. You have a couple of other issues:
if the user inputs an invalid number twice, you accept it the second time
it would be better to pass number_in to validateNumber as a parameter rather than relying on a global
if the user inputs something which is not an integer, the game terminates
Note also you can simplify validateNumber as follows:
def validateNumber(number):
return 1 <= number <= 6
Overall I would rewrite the code as:
from random import randint
def validateNumber(number):
return 1 <= number <= 6
game_1 = randint(1, 6)
while True:
# play game
is_valid = False
while not is_valid:
# read an input
try:
number_in = int(input("Enter a number between 1-6: "))
is_valid = validateNumber(number_in)
except ValueError:
pass
# check the input
if number_in == game_1:
print(f"The result was {game_1}. Your guess was correct!")
# all done
break
else:
print(f"The result was {game_1}. Your guess was NOT correct. Please try again")
# get another number for them to guess
game_1 = randint(1, 6)
After you get correct answer (number_in == game_1), you should break the while.
I think, this is what you want:
from random import randint
def validateNumber():
valid = False
if 1 <= number_in <= 6:
valid = True
return valid
game_1 = randint(1, 6)
guess_was_correct = False
while not guess_was_correct:
is_valid = False
while not is_valid:
try:
number_in = int(input("Enter a number between 1-6: "))
is_valid = validateNumber()
if not is_valid:
print("You entered a number outside of the 1-6 range. Please enter a number between 1 and 6!")
except ValueError:
print("You did not enter a number. Please enter a number.")
if number_in == game_1:
print(f"The result was {game_1}. Your guess was correct!")
guess_was_correct = True
else:
print(f"The result was {game_1}. Your guess was NOT correct. Please try again")
You need an inner loop to check the validity of the input, and the exception handling needs to go there too.
Checking the correctness of the guess is done in the outer loop. It's a separate thing and needs to be done only once we have a valid input.

How to apply nested while loops in python function

I am learning python and trying to build a number guessing game.
The game will randomly generate integer between 1 to 250 (both inclusive) for user to guess. When user input is out of the range, it will prompt user that it is our of range and request another input. Likewise, it will prompt user if it is too high or too low until correct number is guessed. At any point in time if user input 'stop', it will end the game.
As part of the assignment requirement, i need to code 2 new functions on top of those already provided and incorporate it in the final run of the game.
I am trying to convert the while loops below to a function with not much progress. It either end with positional argument error or endless loop.
the_number = random.randint(1,250)
# capture initial user input
user_input = get_user_input()
# repeat code below as long as the user has not guessed the right number
while user_input != the_number:
# repeat code below as long as user has not entered STOP
while user_input != 'STOP':
# verify user input is within range
check_range(user_input)
if user_input < the_number:
slow_print("{} is too low". format(user_input))
print('\n')
user_input = get_user_input()
elif user_input > the_number:
slow_print("{} is too high". format(user_input))
print('\n')
user_input = get_user_input()
else:
slow_print("Congratulations you have guessed the correct number")
break
else:
slow_print('\nGame stopped by user. Thank you for playing')
break
Please can anyone suggest how I can convert the while loops into a function that works.
I'm not certain what you are trying to achieve, but if all you need to do is add a further 2 functions then perhaps this is what you're looking for;
the_number = random.randint(1,250)
# capture initial user input
user_input = get_user_input()
# repeat code below as long as the user has not guessed the right number
while user_input != the_number:
# repeat code below as long as user has not entered STOP
while user_input != 'STOP':
# verify user input is within range
check_range(user_input)
if user_input < the_number:
tooLow(user_input)
user_input = get_user_input()
elif user_input > the_number:
tooHigh(user_input)
user_input = get_user_input()
else:
slow_print("Congratulations you have guessed the correct number")
break
else:
slow_print('\nGame stopped by user. Thank you for playing')
break
def tooLow(number):
slow_print("{} is too low". format(number))
print('\n')
def tooHigh(number):
slow_print("{} is too high". format(number))
print('\n')

Python: random number game with input validation

I'm writing a program that is a guessing game where the user has one chance to guess a number between 1 and 20.
There are two problems with the code:
Number one is still the input validation. The firstGuess variable is actually in main().
firstGuess = userguess()
def userGuess():
while True:
try:
guess = int(input('Enter a number between 1 and 20: '))
if 1 <= guess >= 20:
return guess
except ValueError:
print (guess, 'is not a valid guess!')
break
What I'm trying to do is put the input validation in a loop (while True:) until the user gives good input (in this case a positive number between 1 and 20). If the user were to enter 'd', or '-5', the program should continue looping until good input is given. However, this is not the case. By adjusting the code, I have been able to ask for the input again once bad input is entered, but if bad input is given a third time I get "another exception occured while handling this exception."
*Removed other problem, #Henry Woody was correct in that I wasn't using the conditionals correctly.
The issues are with the conditionals.
The first is the line:
if 1 <= guess >= 20:
in userGuess, which is checking for a number greater than or equal to 20, which is not what you want. You can fix this issue by changing the condition to:
if 1 <= guess <= 20:
Next, the conditional:
if guess1 == randomOne or randomTwo or randomThree:
checks whether guess1 == randomOne or if randomTwo is truthy or if randomThree is truthy. It does not check if guess1 == randomOne or guess1 == randomTwo or guess1 == randomThree as intended. You can fix this by changing the condition to:
if guess1 in [randomOne, randomTwo, randomThree]:
to check if guess1 is equal to either of the three random variables.
Edit:
There is also an issue in the try/except block. If the user enters a non-digit character in the input, a ValueError will be raised before guess is defined. But then guess is referenced in the except block, but guess isn't defined at that point.
You can fix this by getting the user input and then, separately, trying to convert the input to an int.
Here's an example:
while True:
guess = input('Enter a number between 1 and 20: ')
try:
guess = int(guess)
if 1 <= guess <= 20:
return guess
except ValueError:
print (guess, 'is not a valid guess!')
break

How can I make this code less verbose?

I am making a simple 'guess a number between one and ten' game. I have used some basic error handling and am printing the number generated by the random module for testing purposes.
However I would like to know if there is a less verbose way to write this.
This is the code:
import random
while True:
"""Variable declaration"""
number_of_attempts = 1
number = random.randrange (1,11)
print (number)
print("Time to play a guessing game! Muhahaha...")
"""Error handling and main game code/while loop"""
while True:
try:
guess = int(input("Guess a number between one and ten."))
except ValueError:
print("Input a whole number between one and ten silly!")
continue
if guess >= 1 and guess <= 10:
pass
else:
print("Input a number between one and ten silly!")
continue
if guess == number:
print("You were successful and it took you", number_of_attempts, "attempts!!!")
break
else:
print("Try again!")
number_of_attempts = number_of_attempts +1
"""Game Exit/Restart"""
play_again = input("Would you like to play again, y/n?")
if "y" in play_again or "yes" in play_again:
continue
else:
break
Thanks,
Ben
if guess >= 1 and guess <= 10:
Can be written as:
if 1 <= guess <= 10:
Also, your first conditional can simply be written as:
if not 1 <= guess <= 10:
print("Input a number between one and ten silly!")
continue
But this can also be put inside the try bit, saving you from writing continue twice:
try:
guess = int(input("Guess a number between one and ten."))
if not 1 <= guess <= 10:
print("Input a number between one and ten silly!")
continue
except ValueError:
print("Input a whole number between one and ten silly!")
continue
Finally your last conditional can simply be:
if play_again not in ('y', 'yes'):
break
The continue isn't needed.
You may also want to wrap this all up into a function as well, to get rid of those infinite while loops and to prevent you from using continue and break so much.
Why not put the actual conditions on the while loops so you don't have to hunt for breaks to understand the loops? It would make your code clearer and smaller.
if guess == number:
print("You were successful and it took you", number_of_attempts, "attempts!!!")
break
For instance if you put guess == number as the while loop conditional then the print would be the first thing after the loop. Initialize guess to -1 so it always works the first time. The play again if statement could also disappear into the loop conditional as well.

Categories