Reputation: 21
I want to make the "continue playing" user input to only appear when all the tries are exhausted in the guessing game.
from random import random, randint
import random
#Guessing Game
attempts = 5
print("===GUESS THE NUMBER===")
print(f"GAME OBJECTIVE: Guess the computer's number in {attempts} tries")
running = True
computerNum = random.randint(1,15)
while running == True:
guess = int(input("Guess the number:\n>"))
print("\n")
if guess > computerNum:
print("Lower...")
attempts -= 1
print(f"[You have {attempts} tries left]")
if guess < computerNum:
print("Higher...")
attempts -= 1
print(f"[You have {attempts} tries left]")
if attempts == 0:
print("GAME OVER!!!")
print(f"The computer's number is {computerNum}")
break
if guess == computerNum:
print("You have guessed the number!!!")
print("YOU WIN!!!")
break
print("\n")
playAgain = input("Do you want to continue playing(Y/N):\n")
if playAgain in ("N","n"):
break
The problem with this code now is just that each time you guess the number, it keeps asking if you want to continue playing.
Also, I want the console to print "Not a number" each time anything apart from a number is input.
if guess != int():
print("Enter a number")
This is my rough idea so far but I keep getting error messages
I am a beginner by the way
Upvotes: 0
Views: 236
Reputation: 160
Like Tacconinja007 said, you could use a try, except statement to make sure the guess is always a number. And if you want to check if the guess is an integer you could do:
if type(guess) == int:
And if you want to make it so the program asks the user if they want to play again once they run out of attempts you could try the following code:
from random import random, randint
import random
# Guessing Game
attempts = 5
print("===GUESS THE NUMBER===")
print(f"GAME OBJECTIVE: Guess the computer's number in {attempts} tries")
running = True
computerNum = random.randint(1, 15)
while running:
try:
guess = int(input("Guess the number:\n>"))
print("\n")
except ValueError:
print("Enter a number")
continue
if guess > computerNum:
print("Lower...")
attempts -= 1
print(f"[You have {attempts} tries left]")
elif guess < computerNum:
print("Higher...")
attempts -= 1
print(f"[You have {attempts} tries left]")
if attempts == 0:
print("GAME OVER!!!")
print(f"The computer's number is {computerNum}")
replay = input("Do you want to play again? (Y/N): ")
if replay in ("N", "n"):
break
else:
attempts = 5
computerNum = random.randint(1, 15)
if guess == computerNum:
print("You have guessed the number!!!")
print("YOU WIN!!!")
break
print("\n")
Upvotes: 0
Reputation: 11
You can handle user input that is not a number as shown in https://www.geeksforgeeks.org/python-int-function/.
You should not use break
when the user wins or loses.
If you use it, the user is not able to play the game again.
To prevent the program from asking every time whether the user wants to play again, you have to check again for remaining tries if attempts == 0 or guess == computerNum
.
from random import random
import random
# Guessing Game
attempts = 5
print("===GUESS THE NUMBER===")
print(f"GAME OBJECTIVE: Guess the computer's number in {attempts} tries")
running = True
computerNum = random.randint(1, 15)
while running == True:
try:
guess = int(input("Guess the number:\n>"))
except ValueError as e:
print("Enter a number")
continue
print("\n")
if guess > computerNum:
print("Lower...")
attempts -= 1
print(f"[You have {attempts} tries left]")
if guess < computerNum:
print("Higher...")
attempts -= 1
print(f"[You have {attempts} tries left]")
if attempts == 0:
print("GAME OVER!!!")
print(f"The computer's number is {computerNum}")
if guess == computerNum:
print("You have guessed the number!!!")
print("YOU WIN!!!")
if attempts == 0 or guess == computerNum:
print("\n")
playAgain = input("Do you want to continue playing(Y/N):\n")
if playAgain in ("N", "n"):
break
else:
computerNum = random.randint(1, 15)
attempts = 5
Upvotes: 1