I finally got it working! But for some reason the program prints out the previous error statement (too high or too low or please enter values between...), along with the value error message if the user enters in something that the try catches. Could anyone explain why? Any shortening/cleanup is also welcome. Sorry for any errors. Thanks!
'''
This is a guessing game that finds a random number, and then
Tells the user if their guess is too low or too high. It will also give
Error messages for any numbers outside of the accepted range, and will also
Give errors for anything not an integer.
At the end (if the user guesses correctly) it will ask if the
User would like to play again or quit.
'''
import random
def start_here():
print("Welcome to the guessing game!")
play_game()
def play_game():
random_number = random.randrange(1, 100)
correct = False
user_guess = True
while not correct:
try:
user_guess = int(input("Enter your guess: "))
except ValueError:
print("Please only use integers")
if user_guess > 100 or user_guess < 1:
print("Please only enter numbers between 1 and 100!")
elif user_guess > random_number:
print("Too high, try again. ")
elif user_guess < random_number:
print("Too low, try again! ")
elif user_guess == random_number:
break
if user_guess == random_number:
replay = (input("Great! You guessed it! would you like to play again? y or n"))
if replay == "y":
start_here()
else:
print("See ya later!")
start_here()
Keep in mind that the code after the try-except block gets executed irrespective of whether an exception was thrown or not. If the except block gets invoked you want your code to skip through the rest of the statements in the while loop and continue at the next iteration of the loop, where the user is prompted for input again. This can be achieved by using the continue keyword in the except block like so:
try:
user_guess = int(input("Enter your guess: "))
except ValueError:
print("Please only use integers")
continue
The continue statement directs the interpreter to skip the remaining statements in the current iteration of the loop. The flow of control can then re-enter the loop or exit, depending on the loop condition.
Now that your code runs the way it is intended to, here is how you can make it more concise:
Firstly, there is a neat feature in Python which allows you to write conditions like not 1 <= user_guess <= 100. These conditions are much quicker to read, and you can replace this in your code.
Secondly, the start_here() function is redundant. You can easily replace play_game() in its place with a few modifications like so:
import random
def play_game():
print("Welcome to the guessing game!") #Modification here
random_number = random.randrange(1, 100)
correct = False
user_guess = True
while not correct:
try:
user_guess = int(input("Enter your guess: "))
except ValueError:
print("Please only use integers")
continue #Modification here
if not 1<=user_guess<=100: #Modification here
print("Please only enter numbers between 1 and 100!")
elif user_guess > random_number:
print("Too high, try again. ")
elif user_guess < random_number:
print("Too low, try again! ")
elif user_guess == random_number:
break
if user_guess == random_number:
replay = (input("Great! You guessed it! would you like to play again? y or n"))
if replay == "y":
play_game() #Modification here
else:
print("See ya later!")
play_game() #Modification here
or you could entirely replace the play_game() function with a while loop like so:
import random
replay = 'y' #Modification here
while replay == 'y': #Modification here
print("Welcome to the guessing game!")
random_number = random.randrange(1, 100)
correct = False
user_guess = True
while not correct:
try:
user_guess = int(input("Enter your guess: "))
except ValueError:
print("Please only use integers")
continue
if not 1<=user_guess<=100 :
print("Please only enter numbers between 1 and 100!")
elif user_guess > random_number:
print("Too high, try again. ")
elif user_guess < random_number:
print("Too low, try again! ")
elif user_guess == random_number:
break
if user_guess == random_number: #Modification here
replay = input("Great! You guessed it! would you like to play again? y or n")
print("See ya later!") #Modification here
Here:
while not correct:
try:
user_guess = int(input("Enter your guess: "))
except ValueError:
print("Please only use integers")
if user_guess > 100 or user_guess < 1:
# etc
If the user input is not a valid int, you display an error message but still proceed with testing the value against the random number. You should instead skip the rest of the loop, or extract the part getting the user input into it's own loop. As a general rule, one function should do only one thing, then you use another function to control the whole program's flow:
def get_num():
while True:
try:
user_guess = int(input("Enter your guess: ").strip())
except ValueError:
print("Please only use integers")
continue
if user_guess > 100 or user_guess < 1:
print("Please only enter numbers between 1 and 100!")
continue
return user_guess
def again():
replay = input("would you like to play again? y or n"))
return replay.strip().lower() == "y"
def play_game():
random_number = random.randrange(1, 100)
while True:
user_guess = get_num()
if user_guess > random_number:
print("Too high, try again. ")
elif user_guess < random_number:
print("Too low, try again! ")
else:
# if it's neither too high nor too low then it's equal...
break
# no need to test again, we can only get here when the user
# found the number
print("Great! You guessed it!")
def main():
print("Welcome to the guessing game!")
while True:
play_game()
if not again():
break
if __name__ == "__main__":
main()
Related
I have a program that works fine, however I need to make it so that it can execute again when the if statement regarding playing again is satisfied.
import random
n=random.randint(0,10)
print(n)
number= int(input('Guess what the number is'))
count=0
while number !=n:
count=count+1
number= int(input('Guess what the number is'))
if number< n:
print("that is too low")
elif number>n:
print("That is too high")
else:
print("You got it right in"+ " "+str(count+1)+" "+ "tries")
print(count+1)
yesorno= str(input('Do you want to play again? y or n'))
if yesorno=="y":
number= int(input('Guess what the number is'))
elif yesorno=="n":
print("Goodbye")
If you don't want an ugly big while loop, use functions. It makes your code cleaner.
import random
def play():
input("Guess a number between 1 and 10: ")
random_number = random.randint(1, 10)
guess = None
attempts = 0
while guess != random_number:
guess = int(input("Pick a number from 1 to 10: "))
attempts += 1
if guess < random_number:
print("TOO LOW!")
elif guess > random_number:
print("TOO HIGH!")
print("YOU GOT IT! The number was {}, you got it in {} attempts.".format(random_number, attempts))
def main():
play()
while input("Play again? (y/n) ").lower() != "n":
play()
main() # Call the main function
import random
n=random.randint(0,10)
count = 0
while True:
count=count+1
number= int(input('Guess what the number is'))
if number< n:
print("that is too low")
elif number>n:
print("That is too high")
else:
print("You got it right in"+ " "+str(count)+" "+ "tries")
print(count)
yesorno= str(input('Do you want to play again? y or n'))
if yesorno=="y":
n=random.randint(0,10)
count = 0
elif yesorno=="n":
print("Goodbye")
break
import random
n=random.randint(0,10)
print(n)
count=0
while True:
count=count+1
number= int(input('Guess what the number is '))
if number< n:
print("that is too low")
elif number>n:
print("That is too high")
elif number == n:
print("You got it right in"+ " "+str(count+1)+" "+ "tries")
print(count+1)
yesorno= str(input('Do you want to play again? y or n'))
if yesorno=="n":
print("Goodbye")
break
Use a while loop with a condition that will always be true, like while True:.
To stop this infinite loop, use the break statement within the while loop.
If user inputs "y", the loop will continue because it has not been told the break.
I want to use the try-except code block to notify the user not to insert float type values but integers. The code below doesn't throw an error but rather shuts down.
I guess they are logic errors on the try-except block.
userGuess = int(userGuess)
import random
MAX_GUESSES = 5 # max number of guesses allowed
MAX_RANGE = 20 # highest possible number
# show introductionpygame
print("welcome to my franchise guess number game")
print("guess any number between 1 and", MAX_RANGE)
print("you will have a range from", MAX_GUESSES, "guesses")
def playOneRound():
# choose random target
target = random.randrange(1, MAX_RANGE + 1)
# guess counter
guessCounter = 0
# loop fovever
while True:
userGuess = input("take a guess:")
#check for potential errors
try:
userGuess = int(userGuess)
except:
print("sorry, you are only allowed to enter integers thanks!")
# increment guess counter
guessCounter = guessCounter + 1
# if user's guess is correct, congratulate user, we're done
if userGuess == target:
print("you got it la")
print("it only took you", guessCounter, "guess(es)")
break
elif userGuess < target:
print("try again, your guess is too low.")
else:
print(" your guess was too high")
# if reached max guesses, tell answer correct answer, were done
if guessCounter == MAX_GUESSES:
print(" you didnt get it in ", MAX_GUESSES, "guesses")
print("the number was", target)
break
print("Thanks for playing ")
# main code
while True:
playOneRound() # call a function to play one round of the game
goAgain = input("play again?(press ENTER to continue, or q to quit ):")
if goAgain == "q":
break
The task can be split into two steps:
check that the input string can be converted into a number
check that this number is an integer
while True:
userGuess = input("take a guess: ")
try:
userGuess = float(userGuess) # stuff like "asdf", "33ff" will raise a ValueError
if userGuess.is_integer(): # this is False for 34.2
userGuess = int(userGuess)
break # an integer is found, leave the while loop
except ValueError:
pass # just try again
print("sorry, you are only allowed to enter integers thanks!")
in Python3: How do I check for a string within a while loop that expects integers...
Here's my code currently: I want to replace sentinel value "0" with "quit" or "q" instead. How would I do that?
import random
highest = 10
lowest = 1
counter = 0
random_number = random.randint(lowest, highest)
# The randint function is in the random function within the random MODULE imported above.
guess = int(input(f"Guess a number between {lowest} and {highest}. Enter '0' at any time to quit: "))
while guess is not random_number:
if guess == 0:
print("Quitting game.")
break
elif guess < random_number:
counter += 1
guess = int(input("Guess higher: "))
elif guess > random_number:
counter += 1
guess = int(input("Guess lower: "))
if guess == random_number:
print(f"Correct! Well done!! You got it right after {counter} guess(es)!")
I want to change the sentinel value "0" to "q / quit", but not sure how...
guess = int(in.... don't cast it in int, but string.
And then look if the string is "q" or "quit"
If not q or quit then you cast it in int : elif int(guess) < random_number:
You would have to check whether the input is q / quit before converting it to an integer. With int(input(..)), you convert your input to an integer. This will fail if the user inputs a string.
...
while guess is not str(random_number):
if guess == "q" or guess == "quit":
print("Quitting game.")
break
guess = int(guess)
# this might fail if the user did not enter a number
if guess < random_number:
counter += 1
guess = input("Guess higher: ")
elif guess > random_number:
counter += 1
guess = input("Guess lower: ")
if guess == random_number:
print(f"Correct! Well done!! You got it right after {counter} guess(es)!")
You can do one thing:-
A way is you can take input inside the loop and check the input, with break when the user's guess matches the actual number:-
while 1:
guess = input()
if guess == 'q' or guess == 'quit':
print("Quitting game")
break
elif int(guess) < random_numer:
and continue as the rest of your program.
Maybe try this:
import random
highest = 10
lowest = 1
counter = 0
random_number = random.randint(lowest, highest)
guess = input(f"Guess a number between {lowest} and {highest}. Enter 'quit' at any time to quit: ")
while guess is not random_number:
if guess == "quit":
print("Quitting game.")
break
guess = int(guess)
if guess < random_number:
counter += 1
guess = int(input("Guess higher: "))
elif guess > random_number:
counter += 1
guess = int(input("Guess lower: "))
if guess == random_number:
print(f"Correct! Well done!! You got it right after {counter} guess(es)!")
At first, store the input as a string, then check if it equals to 'quit'. If thats not true, convert the string into an integer and proceed normally.
I tried it out and it worked fine, but maybe there is a more elegant way of doing this.
Thanks to all of you. Here is a copy of the code that completely works and is what I was looking for. The key was (like you all mentioned) to
let input be a string initially
check for 'q' or 'quit' immediately.
Then converting guess to an int for the remaining code
Let subsequent input in the if/elif statements be strings.
guess = input(f"Guess a number between {lowest} and {highest}. Enter 'quit' at any time to quit: ")
while guess is not random_number:
if guess.casefold() == "quit" or guess.casefold() == 'q':
print("Quitting game.")
break
guess = int(guess)
if guess < random_number:
counter += 1
guess = input("Guess higher: ")
elif guess > random_number:
counter += 1
guess = input("Guess lower: ")
if guess == random_number:
print(f"Correct! Well done!! You got it right after {counter} guess(es)!")
Thanks again!
Here's the same code with an "invalid input" checker that continues the loop:
guess = input(f"Guess a number between {lowest} and {highest}. Enter 'quit' at any time to quit: ")
while guess is not random_number:
if guess.casefold() == "quit" or guess.casefold() == 'q':
print("Quitting game.")
break
if guess.isnumeric():
guess = int(guess)
if guess < random_number:
counter += 1
guess = input("Guess higher: ")
elif guess > random_number:
counter += 1
guess = input("Guess lower: ")
if guess == random_number:
print(f"Correct! Well done!! You got it right after {counter} guess(es)!")
else:
print("Invalid input. Try again...")
guess = input(f"Guess a number between {lowest} and {highest}. Enter 'quit' at any time to quit: ")
Why wont this stop when 'n' is entered?
I've tried using break on the bottom else but I get errors doing that. That's a common problem I have with break. I have no idea why I'm thrown off with break.
import random
def game():
secret_num = random.randint(1, 10)
guesses = []
while len(guesses) < 5:
try:
guess = int(input("Guess a number between 1 and 10: "))
except ValueError:
print("{} isn't a number".format(guess))
else:
if guess == secret_num:
print("You got it! The number was {}.".format(secret_num))
break
elif guess < secret_num:
print("My number is higher than {}".format(guess))
else:
print("My number is lower than {}".format(guess))
guesses.append(guess)
else:
print("You didn't get it! My number was {}".format(secret_num))
play_again = input("Do you want to play again? Y/n ")
if play_again.lower() != 'Y':
game()
else:
print("Bye!")
game()
You convert play_again to a lower-case letter but compare it to an upper-case letter.
You could simply change it to:
if play_again.lower() != 'n': # 'y' was wrong, right?
game()
else:
print("Bye!")
return # works also without the explicit return but it makes the intention clearer.
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
I wanted to create a guessing game to get more comfortable programming, The user has up to 100 guesses(yes more than enough). If the number is too high or too low it have them type in a new input, if its correct it will print correct.Now I simply want to have it setup to where I ask them would they like to play again. I think I have an idea of to set it up, by separating them into two functions?
I am aware that is not currently a function but should put this as a fucntion and then put my question as an if statement in its own function?
import random
randNum = random.randrange(1,21)
numguesses = 0
while numguesses < 100:
numguesses = numguesses + 1
userguess = int(input("What is your guess [1 through 20]?"))
if userguess < 1:
print("Too Low")
print("Please enter a valid guess [1-20]!")
elif userguess > 20:
print("Too High")
elif userguess == randNum:
print("Correct")
print("you used",numguesses,"number of guesses")
Here's a simple way to do as you asked.I made a function and when you get the thing correct it asks if you want to play again and if you enter "yes" then it resets the vars and runs the loop again. If you enter anything but "yes" then it breaks the loop which ends the program.
import random
def main():
randNum = random.randrange(1,21)
numguesses = 0
while numguesses < 100:
numguesses = numguesses + 1
userguess = int(input("What is your guess [1 through 20]?"))
if userguess < 1:
print("Too Low")
print("Please enter a valid guess [1-20]!")
elif userguess > 20:
print("Too High")
elif userguess == randNum:
print("Correct")
print("you used",numguesses,"number of guesses")
x = input("would you like to play again?")
if x == "yes":
main()
else:
break
main()
Here is another way to do
import random
randNum = random.randrange(1,21)
numguesses = 0
maxGuess = 100
print("Guessing number Game - max attempts: " + str(maxGuess))
while True:
numguesses +=1
userguess = int(input("What is your guess [1 through 20]? "))
if userguess < randNum:
print("Too Low")
elif userguess > randNum:
print("Too High")
else:
print("Correct. You used ",numguesses," number of guesses")
break
if maxGuess==numguesses:
print("Maximum attempts reached. Correct answer: " + str(randNum))
break
import random
randNum = random.randrange(1, 21)
guess = 0
response = ['too low', 'invalid guess', 'too hight', 'correct']
def respond(guess):
do_break = None # is assigned True if user gets correct answer
if guess < randNum:
print(response[0])
elif guess > randNum:
print(response[2])
elif guess < 1:
print(response[1])
elif guess == randNum:
print(response[3])
do_continue = input('do you want to continue? yes or no')
if do_continue == 'yes':
# if player wants to play again start loop again
Guess()
else:
# if player does'nt want to play end game
do_break = True # tells program to break the loop
# same as ''if do_break == True''
if do_break:
#returns instructions for loop to end
return True
def Guess(guess=guess):
# while loops only have accesse to variables of direct parent
# which is why i directly assigned the guess variable to the Fucntion
while guess < 100:
guess -= 1
user_guess = int(input('What is your guess [1 through 20]?'))
# here the respond function is called then checked for a return
# statement (note i don't know wheter this is good practice or not)
if respond(user_guess):
# gets instructions from respond function to end loop then ends it
break
Guess()
Yet another way with two while loops
answer = 'yes'
while answer == 'yes':
while numguesses < 100:
numguesses = numguesses + 1
userguess = int(input("What is your guess [1 through 20]?"))
if userguess < 1:
print("Too Low")
print("Please enter a valid guess [1-20]!")
elif userguess > 20:
print("Too High")
elif userguess == randNum:
print("Correct")
print("you used",numguesses,"number of guesses")
break #Stop while loop if user guest, hop to the first loop with answer var
answer = raw_input("Would you like to continue? yes or no\n>")