I am making a number guessing game, and I have a while loop over the whole thing. The problem is, I can’t get it to stop when I want it to.
import random
import sys
import time
ynChoice = 'x'
global runGTN
runGTN = True
while runGTN == True: #the loop
typing_speed = 300 #wpm
def printSlow(*t):
for l in t:
sys.stdout.write(l)
sys.stdout.flush()
time.sleep(random.random()*10.0/typing_speed)
print()
def confirmChoice(ynChoice):
if ynChoice.lower() == 'yes' or ynChoice.lower() == 'y':
printSlow('Got it!')
ynChoice = 'y'
elif ynChoice.lower() == 'no' or ynChoice.lower() == 'n':
printSlow('Oh. Ok!')
ynChoice = 'n'
else:
printSlow('I don\'t understand. Please input \"yes\" or \"no\".')
def askGuessDifficulty():
printSlow('Alright! I\'ll pick a number from 1 to 100, and you try to guess it!')
printSlow('''You can do easy mode (E), with infinite guess; normal mode (N), with 10 guesses;
hard mode (H), with 5 guesses; or custom mode (C), where you pick how many guesses you get!''')
guessDifficulty = input()
global guesses
if guessDifficulty.lower() == 'e':
guesses = 999
confirmChoice(input('You want easy mode, right? (Please input \"Yes\" or \"No\")'))
elif guessDifficulty.lower() == 'n':
guesses = 10
confirmChoice(input('You want normal mode, right? (Please input \"Yes\" or \"No\")'))
elif guessDifficulty.lower() == 'h':
guesses = 5
confirmChoice(input('You want hard mode, right? (Please input \"Yes\" or \"No\")'))
elif guessDifficulty.lower() == 'c':
confirmChoice(input('You want custom mode, right? (Please input \"Yes\" or \"No\")'))
guesses = input()
if guesses.isnumeric() == True:
printSlow('Ok! You want to have ', guesses, ' guesses, right?')
confirmChoice(input('(Please input \"yes\" or \"no\")'))
if ynChoice == 'n':
return
else:
printSlow('I don\'t understand. Please input a number for the amount of guess that you want.')
else:
printSlow('''I don\'t understand. Please input \"E\" for easy mode, \"N\" for
normal mode, \"H\" for hard mode, and \"C\" for custom mode''')
def playerGuess():
playerGuessWin = False
printSlow('Ok! I\'m thinking of a number from 1 to 100')
printSlow('You\'ll have ', guesses, ' guesses to get it.')
printSlow('Go ahead and guess when you\'re ready.')
min = 1
max = 100
playerGuessNum = random.randint(min,max)
while playerGuessWin == False:
currentGuess = input()
if currentGuess.isnumeric() == True and int(currentGuess) <= 100 and int(currentGuess) >= 1:
printSlow('Your guess is...')
if currentGuess == playerGuessNum:
printSlow('CORRECT!')
else:
printSlow('Not right :(')
else:
printSlow('I don\'t understand. Please enter a number from 1 to 100.')
def guessTheNumberStart():
printSlow('You want to play Guess The Number, right?')
confirmChoice(input('(Please input \"yes\" or \"no\")'))
if ynChoice == 'y':
printSlow('Alright! Let\'s do it!')
elif ynChoice == 'n':
printSlow('Oh. Ok!')
global runGTN
runGTN = False #this is where it's supposed to stop the while loop
return #I tried using "break" but it said that it wasn't inside a loop. It just continues on for some reason.
printSlow('Do you want to guess my number, or should I guess yours?')
printSlow('(If you want to guess, input \"I\". If I should guess, input \"U\".)')
whoGuess = input()
if whoGuess.lower() == 'i':
printSlow('Ok, you guess!')
askGuessDifficulty()
playerGuess()
elif whoGuess.lower() == 'u':
printSlow('Ok, I\'ll guess!')
else:
printSlow('I don\'t understand. Please input \"I\" if you should guess and input \"U\" if I should guess.')
guessTheNumberStart()