Asking user whether he/she wants to play again but typing yes just repeats this question again rather starting again
It is a number guessing game
Explanation
At first it asks the user to enter a number between 1 to 50 Then if the number is correct then you win else you have to try again (The winning number is random offcourse)You also have limited guesses
The problem is mentioned below the code Here is my code:)
import random
winning_num = 23
guesses = 1
guesses_left = 9
game_over = False
end_game = False
number_enter = False
while not end_game:
while not number_enter:
try:
ask = int(input("ENTER A NUMBER BETWEEN 1 AND 50: "))
print(f"TOTAL GUESSES = {guesses_left}")
break
except ValueError:
print("INVALID INPUT!!")
continue
while not game_over:
if ask==winning_num:
print(f"YOU WON BY GUESSING THE NUMBER IN {guesses} TIME(S)!!")
print("DO YOU WANT TO PLAY AGAIN?")
while True:
ask1 = input("ENTER 'YES' OR 'NO' ONLY: ")
ask1 = ask1.lower()
if ask1=='yes':
print("YOU CHOSE TO PLAY AGAIN")
game_over = False
break
elif ask1=="no":
print("THANK YOU FOR PLAYING THIS GAME")
game_over = True
end_game = True
break
else:
print("PLEASE WRITE 'YES' OR 'NO' ONLY ")
continue
elif ask>winning_num:
print("TOO HIGH!!")
guesses+=1
guesses_left-=1
while True:
try:
ask = int(input("TRY AGAIN: "))
print(f"GUESSES LEFT = {guesses_left}")
break
except ValueError:
print("INVALID INPUT!!")
continue
if guesses_left==1:
print("ONLY ONE GUESS LEFT!!")
continue
elif guesses_left==0:
print("YOU LOSE!!")
break
elif ask<winning_num:
print("TOO LOW!!")
guesses+=1
guesses_left-=1
while True:
try:
ask = int(input("TRY AGAIN: "))
print(f"GUESSES LEFT = {guesses_left}")
break
except ValueError:
print("INVALID INPUT!!")
continue
if guesses_left==1:
print("ONLY ONE GUESS LEFT!!")
continue
elif guesses_left==0:
print("YOU LOSE!!")
break
The problem is when the game ends It asks whether we want to play again But if we type "Yes" it again asks the same "Do you want to play again" However typing "No" works fine and the program ends
Solution 1:
you have to set game_over = False in case ask1 = yes so that it can come out of the parent while loop and proceed. Also, you'll have to reset number of guesses etc so that it starts as a new game.
Solution 2:
import random
winning_num = 23
guesses = 1
guesses_left = 9
game_over = False
end_game = False
number_enter = False
while not end_game:
while not number_enter:
try:
ask = int(input("ENTER A NUMBER BETWEEN 1 AND 50: "))
print(f"TOTAL GUESSES = {guesses_left}")
break
except ValueError:
print("INVALID INPUT!!")
continue
while not game_over:
if ask==winning_num:
print(f"YOU WON BY GUESSING THE NUMBER IN {guesses} TIME(S)!!")
print("DO YOU WANT TO PLAY AGAIN?")
while True:
ask1 = input("ENTER 'YES' OR 'NO' ONLY: ")
ask1 = ask1.lower()
if ask1=='yes':
print("YOU CHOSE TO PLAY AGAIN")
game_over = True
break
elif ask1=="no":
print("THANK YOU FOR PLAYING THIS GAME")
game_over = True
end_game = True
break
else:
print("PLEASE WRITE 'YES' OR 'NO' ONLY ")
continue
elif ask>winning_num:
print("TOO HIGH!!")
guesses+=1
guesses_left-=1
while True:
try:
ask = int(input("TRY AGAIN: "))
print(f"GUESSES LEFT = {guesses_left}")
break
except ValueError:
print("INVALID INPUT!!")
continue
if guesses_left==1:
print("ONLY ONE GUESS LEFT!!")
continue
elif guesses_left==0:
print("YOU LOSE!!")
break
elif ask<winning_num:
print("TOO LOW!!")
guesses+=1
guesses_left-=1
while True:
try:
ask = int(input("TRY AGAIN: "))
print(f"GUESSES LEFT = {guesses_left}")
break
except ValueError:
print("INVALID INPUT!!")
continue
if guesses_left==1:
print("ONLY ONE GUESS LEFT!!")
continue
elif guesses_left==0:
print("YOU LOSE!!")
break