mardi 28 avril 2020

Random Number guessing game with 2 option menu

I am writing a random number generator that prompts the user for a guess. I want the program to only terminate when option 2 is chosen. So far all of my outputs are correct but when they get the guess right, it just jumps straight into a new game. It will not give the desired output. I am sure I am just overlooking a small detail in the while loop but I have tried what I am familiar with. Any help would be great. Below is what I have so far:

'''

# Import the Random function.
import random

# Create variable to keep track of guesses
count = 0

# Define menu for ease of reference

menu = """
1. Play Game
2. Exit
"""

choice = None

# Set the Loop

while (choice != 2):
    print (menu)
    choice = int(input("Would you like to play a game?"))

    if (choice == 1):
        print ("Guess a number between 1 & 100:")
        x = random.randint (1, 100)
        guess = int(input())

        while (guess != x):
            if (guess < x):
                guess = int(input("Your guess is too low, try again:"))
                count = count+1

            elif (guess > x):
                guess = int(input("Your guess is too high, try again:"))
                count = count+1

            elif (guess == x):
                print ("Congratulations, you guessed the number in", count, "attempts!")



    else:
        exit

#Pseudocode for the above program

# Ask user if they want to play the guessing game
# Randomly generate a number
# Ask user to guess and give hints (too low, too high)
# Generate a new random number each game
# Only quit when user selects to exit

'''




Aucun commentaire:

Enregistrer un commentaire