This question already has an answer here:
I'm still new to Python and am right now building the general get-into-it guessing game. I've tried to include a question for the user if they wish to continue, but sadly it does not seem to recognize my set answers.
def game():
numofguesses = 0
number = 10
while numofguesses < 5:
guess = int(input("Enter a number: "))
numofguesses = numofguesses + 1
if guess < number:
print("Higher")
elif guess > number:
print("Lower")
elif guess == number:
break
if guess == number:
print("You've won in " + str(numofguesses) + " guesses.")
else:
print("The correct number was " + str(number) + " and sadly you didnt get it!")
print("Hello, this game lets you guess a random number!")
go_again = True
while go_again:
game()
again = input("Do you want to play again? Y/N: ")
if again == "y" or "Y":
go_again = True
elif again == "n" or "N":
go_again = False
else:
print("unknown entry.")
It works fine when I want to continue and enter "y", but even if I want to quit and enter "n", I get the same result, the program continues.
Hello, this game lets you guess a random number!
Enter a number: 10
You've won in 1 guesses.
Do you want to play again? Y/N: y
Enter a number: 10
You've won in 1 guesses.
Do you want to play again? Y/N: n
Enter a number: 10
You've won in 1 guesses.
Do you want to play again? Y/N:
I think I'm missing something super easy, but can't for the life of me figure out what it is.
Thanks a lot in advance for any help!
Greetings
Aucun commentaire:
Enregistrer un commentaire