Home > Blockchain >  Everything else in my (very basic) code works except for the code restarting
Everything else in my (very basic) code works except for the code restarting

Time:01-16

I've just begun learning how to code so I'm working on some pretty basic projects so far, bear with me :) Today's project is a very simple number guessing game and I have everything figured out except for one part. (The problem area is under "#Play again" in my code.)

What I want, is when someone has finished the game for them to be able to answer the question "Would you like to play again?" with yes or no. If they say yes, it will restart the game. If they say no, it will end the code. As it is right now, when you answer "no" the game will stop. However, if you answer "yes" the game will not restart and instead will ask "Would you like to play again?" over and over.

Any help would be greatly appreciated as I've tried it many different ways and I can't seem to figure it out. Thank you!

import random


#introduce variables 
number = random.randint(1, 100)
guess = None
name = input("What is your name?\n")
turns = 10


#name
print(f"Hello, {name}, let's play a game!")


#loop 
while True:



    while guess != number:

        guess = input("Guess a number between 1 and 100: ")

        guess = int(guess)


        if guess == number:

            print(f"Congratulations, {name}! You won with {turns} turn(s) left!")

        elif guess > number:
            turns -= 1
            print(f"Sorry, your guess is too high! You have {turns} turn(s) left. Guess again.")

        elif guess < number:
            turns -= 1
            print(f"Sorry, your guess is too low! You have {turns} turn(s) left. Guess again.")

        if turns == 0:
            print("Sorry, {name}, you lose :(")

#Play again  
    again = input(f"Would you like to play again, {name}? yes or no.\n")

    if "yes" in again:
        print(f"Let's see if you can get it again, {name}!")
        continue

    elif "no" in again:
        print(f"Thanks for playing, {name}!")
        break

    else:
        print(f"Sorry, {name}, please enter yes or no.")

CodePudding user response:

You need to initialize the parameter values every time the game starts, and end it every success or failure, ie exit the inner loop.

import random


#introduce variables 
name = input("What is your name?\n")

#name
print(f"Hello, {name}, let's play a game!")

#loop 
while True:
    number = random.randint(1, 100)
    guess = None
    turns = 10

    while guess != number:

        guess = input("Guess a number between 1 and 100: ")

        guess = int(guess)


        if guess == number:

            print(f"Congratulations, {name}! You won with {turns} turn(s) left!")
            break

        elif guess > number:
            turns -= 1
            print(f"Sorry, your guess is too high! You have {turns} turn(s) left. Guess again.")

        elif guess < number:
            turns -= 1
            print(f"Sorry, your guess is too low! You have {turns} turn(s) left. Guess again.")

        if turns == 0:
            print("Sorry, {name}, you lose :(")
            break

    #Play again  
    again = input(f"Would you like to play again, {name}? yes or no.\n")

    if "yes" in again:
        print(f"Let's see if you can get it again, {name}!")
        continue

    elif "no" in again:
        print(f"Thanks for playing, {name}!")
        break

    else:
        print(f"Sorry, {name}, please enter yes or no.")

CodePudding user response:

This is working fine

import random


#introduce variables 
number = random.randint(1, 100)
guess = None
name = input("What is your name?\n")
turns = 10


#name
print(f"Hello, {name}, let's play a game!")


#loop 
while True:



    while guess != number:

        guess = input("Guess a number between 1 and 100: ")

        guess = int(guess)


        if guess == number:

            print(f"Congratulations, {name}! You won with {turns} turn(s) left!")

        elif guess > number:
            turns -= 1
            print(f"Sorry, your guess is too high! You have {turns} turn(s) left. Guess again.")

        elif guess < number:
            turns -= 1
            print(f"Sorry, your guess is too low! You have {turns} turn(s) left. Guess again.")

        if turns == 0:
            print("Sorry, {name}, you lose :(")
            break;
#Play again  
    again = input(f"Would you like to play again, {name}? yes or no.\n")

    if "yes" in again:
        print(f"Let's see if you can get it again, {name}!")
        continue

    elif "no" in again:
        print(f"Thanks for playing, {name}!")
        break

    else:
        print(f"Sorry, {name}, please enter yes or no.")
  •  Tags:  
  • Related