Home > Back-end >  Python random number guessing game
Python random number guessing game

Time:07-17

    I'm having issues with this random number guessing game. There are 2 issues: The first issue has to do with the counting of how many tries you have left. it should give you 3 changes but after the 2nd one it goes into my replay_input section where I am asking the user if they want to play again. 
        import random
        # guess the # game
        guess = input("Enter in your numerical guess. ")
        random_number = random.randint(0, 10)
        print(random_number)            # used to display the # drawn to check if code works
        number_of_guess_left = 3
        # this is the main loop where the user gets 3 chances to guess the correct number 
    
        while number_of_guess_left > 0:
            if guess != random_number:
                number_of_guess_left -= 1
                print(f"The number {guess} was an incorrect guess. and you have {number_of_guess_left} guesses left ")
                guess = input("Enter in your numerical guess. ")
            elif number_of_guess_left == 0:
                print("You lose! You have no more chances left.")
            else:
                print("You Win! ")
            break

The second part has to do with the replay input, I can't seem to get it to loop back to the beginning to restart the game.

        replay_input = input("Yes or No ").lower()
        if replay_input == "yes":
            guess = input("Enter in your numerical guess. ")

CodePudding user response:

Please focus on the basics first before posting the questions here. Try to debug with tools like https://thonny.org/. However, I updated your code, just check.

import random
# guess the # game
random_number = random.randint(0, 10)
print(random_number) 
# don't forget to convert to int       
guess = int(input("Enter in your numerical guess. "))
number_of_guess_left = 3
# this is the main loop where the user gets 3 chances to guess the correct number 

while number_of_guess_left > 0:
    number_of_guess_left -= 1
    if guess == random_number:
      print("You Win! ")
      break
    else:
      if number_of_guess_left == 0:
          print("You lose! You have no more chances left.")
          break
      else:
          print(f"The number {guess} was an incorrect guess. and you have {number_of_guess_left} guesses left ")
          guess = int(input("Enter in your numerical guess. "))

CodePudding user response:

The break statement exits a while loop. The code in the loop executes once, hits break at the end, and moves on to execute the code after the loop.

You can have the player replay the game by wrapping it in a function which I've called play_game below. The while True loop at the end (which is outside of play_game) will loop until it encounters a break statement. The player plays a game once every loop. The looping stops when they enter anything other than "yes" at the replay prompt which will make it hit the break statement.

import random

def play_game():
    # guess the # game
    guess = input("Enter in your numerical guess. ")
    random_number = random.randint(0, 10)
    print(random_number)            # used to display the # drawn to check if code works
    number_of_guess_left = 3
    # this is the main loop where the user gets 3 chances to guess the correct number 

    while number_of_guess_left > 0:
        if guess != random_number:
            number_of_guess_left -= 1
            print(f"The number {guess} was an incorrect guess. and you have {number_of_guess_left} guesses left ")
            guess = input("Enter in your numerical guess. ")
        elif number_of_guess_left == 0:
            print("You lose! You have no more chances left.")
        else:
            print("You Win! ")


while True:
    play_game()
    replay_input = input("Yes or No ").lower()
    if replay_input != "yes":
        break

CodePudding user response:

This is work but it's a simple code :

import random

number_of_guess_left = 3
rand_counter = 0;

while number_of_guess_left > 0:
    if (rand_counter == 0):
        random_number = random.randint(0, 10)
        print(f"rand no:  {random_number} dd")
    guess = int(input("Enter in your numerical guess : "))
    if guess != random_number:
        number_of_guess_left -= 1
        if number_of_guess_left != 0:
            print(f"The number {guess} was an incorrect guess. and you have {number_of_guess_left} guesses left ")
            rand_counter  = 1;
            continue;
        else:
            print("You lose! You have no more chances left, Do you want to play again? ")
            is_play_again = input(" Y or N :")
            if is_play_again.__eq__("y"):
                rand_counter = 0;
                number_of_guess_left = 3;
                continue
            elif is_play_again.__eq__("n"):
                print("Game Over");
                break;
    elif random_number == guess:
        print("You Win! ")
    break
  • Related