简体   繁体   中英

Guessing Game - Prompt the user to play again

I'm making a guessing game, but I want to add another line of code where the user can play again after, but I don't know where to start.

print ("Welcome to the Number Guessing Game in Python!")

# Initialize the number to be guessed
number_to_guess = 7

# Initialize the number of tries the player has made
count_number_of_tries = 1

# Obtain their initial guess
guess = int (input ("Please guess a number between 1 and 10: "))
while number_to_guess != guess:
    print ("Sorry wrong number!")
              
    # Check to see they have not exceeded the maximum number of attempts if so break out of loop
    if count_number_of_tries == 3:
        break
    elif guess < number_to_guess:
        print ("Your guess was lower than the number.")
    else:
        print ("Your guess was higher than the number.")

    # Obtain their next guess and increment number of attempts
    guess = int(input ("Please guess again: "))
    count_number_of_tries += 1

# Check to see if they did guess the correct number
if number_to_guess == guess:
    print ("Well done you won!")
    print ("You took" + str(count_number_of_tries) + "attempts to complete the game.")
else:
    print ("Sorry, you lose")
    print ("The number you needed to guess was " + str(number_to_guess) + "." )

print ("Game Over.")

I dont know if the code should be at the bottom or in between somewhere. I also want to remove the break if possible.

Great idea! In my mind, there are two ways of continuously asking the user to play until they enter something that will let you know they want to stop like 'q', 'quit', or 'exit'. First you could nest all of your current code a while loop (indent and write 'while(): before the code) , or turn your current code into a function, and call it over and over again until the user enters the exit command

Ex: lets say i can print hello world like this

# your game here

but now I want to keep printing it until the user enters 'quit'

user_input = '.' # this char does not matter as long as it's not 'quit'
while user_input != 'quit': # check if 'user_input' is not 'quit'
    # your game here

    user_input = input('Press any key to continue (and enter) or write "quit" to exit: ') # prompt user to enter any key, or write quit

I think this should do it, just a basic while loop

playAgain = 'y'
while playAgain == 'y':
    print ("Welcome to the Number Guessing Game in Python!")
    # Initialize the number to be guessed
    number_to_guess = 7
    # Initialize the number of tries the player has made
    count_number_of_tries = 1
    # Obtain their initial guess
    guess = int (input ("Please guess a number between 1 and 10: "))
    while number_to_guess != guess:
              print ("Sorry wrong number!")
              
    # Check to see they have not exceeded the maximum number of attempts if so break out of loop
              if count_number_of_tries == 3:
                  print("You've guessed more than three times, you're pretty bad")
              elif guess < number_to_guess:
                         print ("Your guess was lower than the number.")
              else:
                         print ("Your guess was higher than the number.")
    # Obtain their next guess and increment number of attempts
              guess = int(input ("Please guess again: "))
              count_number_of_tries += 1
    # Check to see if they did guess the correct number
    if number_to_guess == guess:
         print ("Well done you won!")
         print ("You took " + str(count_number_of_tries) + " attempts to complete the game.")
    else:
        print ("Sorry, you lose")
        print ("The number you needed to guess was " + str(number_to_guess) + "." )
    print ("Game Over.")
    playAgain = (input ("Type y to play again, otherwise press enter: "))
print("Goodbye gamer")

Put all of that code in a game() function. Underneath, write

while True:
    game()
    play=int(input('Play again? 1=Yes 0=No : '))
    if play == 1:
        pass
    else:
        break

print("Bye!")

You can add a while loop to the outermost layer that randomly generates the value of number_to_guess on each startup. And ask if you want to continue at the end of each session.

import random

print("Welcome to the Number Guessing Game in Python!")

number_range = [1, 10]
inp_msg = "Please guess a number between {} and {}: ".format(*number_range)
while True:
    # Initialize the number to be guessed
    number_to_guess = random.randint(*number_range)
    # Initialize the number of tries the player has made
    count_number_of_tries = 1
    # Obtain their initial guess
    guess = int(input(inp_msg))
    while number_to_guess != guess:
        print("Sorry wrong number!")

        # Check to see they have not exceeded the maximum number of attempts if so break out of loop
        if count_number_of_tries == 3:
            break
        elif guess < number_to_guess:
            print("Your guess was lower than the number.")
        else:
            print("Your guess was higher than the number.")
        # Obtain their next guess and increment number of attempts
        guess = int(input("Please guess again: "))
        count_number_of_tries += 1
    # Check to see if they did guess the correct number
    if number_to_guess == guess:
        print("Well done you won!")
        print("You took" + str(count_number_of_tries) + "attempts to complete the game.")
    else:
        print("Sorry, you lose")
        print("The number you needed to guess was " + str(number_to_guess) + ".")

    if input("Do you want another round? (y/n)").lower() != "y":
        break
print("Game Over.")

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