prongramingavo
prongramingavo

Reputation: 63

Python Random number guesser problem with loops

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.

Upvotes: 0

Views: 74

Answers (3)

ITSME
ITSME

Reputation: 36

I just added the "again" line of code in while loop and added an else statement

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)
    
    again = input("Would you like to try again? => ")
    if again.lower() == 'exit':
      break
    else:
        continue

Upvotes: 1

ITSME
ITSME

Reputation: 36

Here is what you asked and i also added turns to track the player

import random
turns = 0
def quit():
    i = input ("Do you want to play again? if yes enter Yes if not Enter No\n")
    if (i.lower() not in ["yes","no"]):
        print ("Invalid input")
        return True
    if (i.lower() == "yes"):
        print ("You choose to play")
        return False
    else:
        print ("Thankyou for playing")
        return True
while True:
    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)
    
    if quit():
        print("You have used",turns,"trun(s)")
        break
    else:
        turns += 1
        continue

Upvotes: 1

Krishna Chaurasia
Krishna Chaurasia

Reputation: 9580

You are missing the statement to take user input again in the while loop:

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)
    again = input("Would you like to try again? => ")
    if again.lower() == 'exit':
    break

For keeping the count, you can add a new variable and increment in the while loop.

Upvotes: 1

Related Questions