I have built a random number guessing game for practice, but I am having some trouble with the final steps. When outputted, the game works as expected, however, I want it to ask the user if they want to play again after every guessing turn, with 'yes' meaning the game keeps going and 'exit' meaning the game stops. As of now, the game asks the user to guess the number, tells user if said number does not match, and then asks if they want to play, then it just repeats the guessing part, without asking if the user wants to play again. This is not what I want, as I would like to know how to properly write this code. Here is my program:
import random
guess = int(input("Guess the number => "))
rand = random.randrange(1,10)
print("The number was", rand)
def guess_rand(guess, rand):
if rand == guess:
print("You have guessed right!")
elif rand > guess:
print("You guessed too low!")
elif guess > rand:
print("You guessed too high!")
guess_rand(guess, rand)
again = input("Would you like to try again? => ")
while again.lower() == 'yes':
guess = int(input("Guess the number => "))
rand = random.randrange(1,10)
print("The number was", rand)
guess_rand(guess, rand)
if again.lower() == 'exit':
break
Also, if there are any tips on how to keep track of how many guesses the user has taken, and when the game ends, to print them out, I would appreciate that. Thank you.
Aucun commentaire:
Enregistrer un commentaire