Я пишу генератор случайных чисел, который предлагает пользователю угадать. Я хочу, чтобы программа завершалась только при выборе варианта 2. Пока что все мои выводы верны, но когда они делают правильное предположение, он просто переходит прямо в новую игру. Это не даст желаемый результат. Я уверен, что я просто упускаю из виду небольшую деталь в то время как я oop, но я попробовал то, что мне знакомо. Любая помощь будет отличной. Вот что у меня есть:
'' '
# 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
' ''