简体   繁体   中英

I am trying to replay the game when I run out of guesses (Play again when I run out of gusses or find the right answer)

My goal is to be able to make the code run again when i finish playing the game
automatically. At the bottom of the code i tried writing a code that will rerun the game but, it seems to have a syntax error

import random 

MAX_GUESSES = 5 #max number of guesses allowed

MAX_RANGE = 20 #highest possible number

#show introductionpygame
print("welcome to my franchise guess number game")
print("guess any number between 1 and", MAX_RANGE)
print("you will have a range from", MAX_GUESSES, "guesses")

#choose random target
target = random.randrange(1, MAX_RANGE + 1)

#guess counter
guessCounter = 0

#loop fovever
while True:
      userGuess = input("take a guess:")
      userGuess =int(userGuess)

#increment guess counter
guessCounter = guessCounter + 1

#if user's guess is correct, congratulate user, we're done
if userGuess == target:
        print("you got it la")
        print("it only took you",guessCounter, "guess(es)")
        break
elif userGuess < target:
        print("try again, your guess is too low.")
else:
        print(" your guess was too high")

#if reached max guesses, tell answer correct answer, were done
if guessCounter == MAX_GUESSES:

        print(" you didnt get it in ", MAX_GUESSES, "guesses")
        print("the number was", target)
        break
    
print("Thanks for playing ")


#main code
while True:
    playOneRound() #call a function to play one round of the game
    goAgain = input("play again?(press ENTER to continue, or q to quit ):")
    if goAgain == "q":
        break

you forgot to mention the function name playOneRound . The code below works fine.

import random

MAX_GUESSES = 5  # max number of guesses allowed

MAX_RANGE = 20  # highest possible number

# show introductionpygame
print("welcome to my franchise guess number game")
print("guess any number between 1 and", MAX_RANGE)
print("you will have a range from", MAX_GUESSES, "guesses")

def playOneRound():
    # choose random target
    target = random.randrange(1, MAX_RANGE + 1)

    # guess counter
    guessCounter = 0

    # loop fovever
    while True:
        userGuess = input("take a guess:")
        userGuess = int(userGuess)

        # increment guess counter
        guessCounter = guessCounter + 1

        # if user's guess is correct, congratulate user, we're done
        if userGuess == target:
            print("you got it la")
            print("it only took you", guessCounter, "guess(es)")
            break
        elif userGuess < target:
            print("try again, your guess is too low.")
        else:
            print(" your guess was too high")

        # if reached max guesses, tell answer correct answer, were done
        if guessCounter == MAX_GUESSES:
            print(" you didnt get it in ", MAX_GUESSES, "guesses")
            print("the number was", target)
            break

        print("Thanks for playing ")

    # main code

while True:
    playOneRound()  # call a function to play one round of the game
    goAgain = input("play again?(press ENTER to continue, or q to quit ):")
    if goAgain == "q":
        break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM