# This is an advanced guess the number game.
import random # The very basis of this game.
import math # Used for math.ceil
import time # Used to split up long portions of text.
def errorDisplay(errorCode):
if errorCode == '1':
print('\n\n\nError ' + errorCode + ': unlockedGameModes outside of expected scope on line 36.')
def introDisplay(): # Displays the intro and defines the playerName Var. at the local scope, then returns it to the global scope.
print('The Guessing Game! Now with more frustration!')
playerName = input('\nHello, what is your name? ')
print('\nWelcome ' + playerName + '.')
print('\nThis is a guessing game with three different modes. It also has two different')
print('settings for each mode. The range in which you will be guessing numbers, and')
print('the amount of attempts you will have. You will be playing through the modes in')
print('order, but you can always return to a game mode you\'ve unlocked.')
#time.sleep(5)
if playerName == 'Debug':
gameModesUnlocked = input('\nEnter number of modes unlocked. <1/2/3> ')
gameModesUnlocked = int(gameModesUnlocked)
else:
gameModesUnlocked = 1
return playerName, gameModesUnlocked
def playGame(playerName, gameModesUnlocked):
if gameModesUnlocked == 1:
print('\nOk, ' + playerName + ' Let\'s play the first mode: Simple guessing game!')
gameMode = 1
elif gameModesUnlocked >= 2 and gameModesUnlocked <= 3:
print('\nOk, ' + playerName + ' choose which mode you would like to play.')
gameMode = chooseGameMode(gameModesUnlocked)
else:
errorCode = '1'
return errorCode
rangeLower, rangeHigher, rangeLowerString, rangeHigherString = chooseRange()
attemptsNumber, attemptsNumberString = chooseAttempts()
def chooseGameMode(gameModesUnlocked): # Here the player chooses the game mode and the chosen mode is returned to the playGame function.
if gameModesUnlocked == 2:
tempInput = input('The simple guessing game, or the one with addition and subtraction? <1/2> ')
while True:
if len(tempInput) == 1 and tempInput in '12':
gameMode = int(tempInput)
break
else:
tempInput = input('Please choose 1 or 2. ')
elif gameModesUnlocked == 3:
tempInput = input('The simple guessing game, the one with addition and subtraction, or the one with more advanced maths? <1/2/3> ')
while True:
if len(tempInput) == 1 and tempInput in '123':
gameMode = int(tempInput)
break
else:
tempInput = input('Please choose 1, 2 or 3. ')
else:
errorCode = 0
gameMode = 0
confirmGameMode(gameMode)
return gameMode
def confirmGameMode(gameMode): # Used to confirm that the player is happy with their game mode setting. Called during the chooseGameMode function.
if gameMode == 1:
print('\nYou have chosen mode the first: Simple guessing game.', end=' ')
elif gameMode == 2:
print('\nYou have chosen mode the second: Guessing game with addition and subtraction.')
elif gameMode == 3:
print('\nYou have chosen mode the third: Guessing game with more advanced maths.')
tempAnswer = input('Continue? <y/n> ').lower()
while True:
if tempAnswer == 'y':
print('Ok!')
return gameMode
elif tempAnswer == 'n':
print('\nThen, let\'s try again.')
tempAnswer = '' # Dear daniweb: Kind of proved it "Please make a valid choice. <y/n>"
gameMode = chooseGameMode(gameModesUnlocked)
elif len(tempAnswer) != 1 or tempAnswer not in 'yn':
tempAnswer = input('Please make a valid choice. <y/n> ').lower()
def chooseRange(): # Here the player chooses between 3 pre-set ranges to play in, or defines their own, via the defineRange function.
rangeLower = 1
rangeLowerString = '1' # These are defined here in the case that the user does not make their own range, they are the same in the three pre-sets.
chosenRangeType = input('\nWould you like a small, medium or large range? Or perhaps you\'d like to\nchoose the range yourself? <s/m/l/*> ').lower()
while True:
if chosenRangeType == 's': # This section accepts input for the pre-sets and break out of the main input loop, ending this function.
rangeHigher = 20
rangeHigherString = '20'
break
elif chosenRangeType == 'm':
rangeHigher = 40
rangeHigherString = '40'
break
elif chosenRangeType == 'l':
rangeHigher = 60
rangeHigherString = '60'
break
elif len(chosenRangeType) != 1: # This section handles erroneous input for the main input loop.
chosenRangeType = input('Please enter a single character. <s/m/l/*> ').lower()
elif chosenRangeType not in 'sml*':
chosenRangeType = input('Please make a valid choice. <s/m/l/*> ').lower()
elif chosenRangeType == '*': # Finally, this section handles the users that would like to define their own range. New input loop, and error handling.
tempAnswer = input('Really choose your own range? <y/n> ').lower()
while True:
if tempAnswer == 'y':
rangeLower, rangeHigher, rangeLowerString, rangeHigherString = defineRange()
break
elif tempAnswer == 'n':
print('Then, let\'s try again.')
rangeLower, rangeHigher, rangeLowerString, rangeHigherString = chooseRange()
elif len(tempAnswer) != 1:
tempAnswer = input('Please enter a single character. <y/n> ').lower()
elif tempAnswer not in 'yn':
tempAnswer = input('Please make a valid choice. <y/n> ').lower()
break
confirmRange(rangeLowerString, rangeHigherString)
return rangeLower, rangeHigher, rangeLowerString, rangeHigherString
def defineRange(): # Called when the player decides to define their own range to play in. Called during the chooseRange function.
tempInput = input('Please choose the lower number in your range. (1-9980) ')
while True:
try:
rangeLower = int(tempInput)
except ValueError:
tempInput = input('Please enter a number between 1 and 9980. ')
if rangeLower in range(1,9981):
rangeLowerString = str(tempInput)
break
else:
tempInput = input('Please enter a number between 1 and 9980. ')
tempInput = input('Please choose the higher number in your range. (20-10000) ')
while True:
try:
rangeHigher = int(tempInput)
except ValueError:
tempInput = input('Please enter a number between 20 and 10000. ')
if rangeHigher in range(20,10001):
if rangeLower >= rangeHigher:
tempInput = input('The second number should be higher than the first, try again. (20-10000)')
else:
rangeHigherString = str(tempInput)
break
else:
tempInput = input('Please enter a number between 20 and 10000. ')
return rangeLower, rangeHigher, rangeLowerString, rangeHigherString
def confirmRange(rangeLowerString, rangeHigherString): # Used to confirm that the player is happy with their range setting. Called during the chooseRange function.
tempAnswer = input('\nYour range will be between ' + rangeLowerString + ' and ' + rangeHigherString + '. Are you happy with this? <y/n> .').lower()
while True:
if tempAnswer == 'y':
print('Ok!')
return rangeLowerString, rangeHigherString
elif tempAnswer == 'n':
print('Then, let\'s try again.')
rangeLower, rangeHigher, rangeLowerString, rangeHigherString = chooseRange()
elif len(tempAnswer) != 1:
tempAnswer = input('Please enter a single character. <y/n> ').lower()
elif tempAnswer not in 'yn':
tempAnswer = input('Please make a valid choice. <y/n> ').lower()
######################################################################## PROGRAM START
playerName, gameModesUnlocked = introDisplay()
errorCode = playGame(playerName, gameModesUnlocked)
errorDisplay(errorCode)
This is my code for a guess the number game I'm making. I'd just been working on the confirmGameMode function and was testing it when I came up with some strange output:
You have chosen mode the second: Guessing game with addition and subtraction.
Continue? <y/n> y
Ok!
Please make a valid choice. <y/n> y
Ok!
Would you like a small, medium or large range? Or perhaps you'd like to
choose the range yourself? <s/m/l/*>
Just after the line of code that prints 'Ok' is a return keyword, so why is my output displaying output from further down that while loop? And more than that, how can it be displaying code from different if/elif statements, when it's not meant to be possible for a variable to be in two states?
steps to reproduce: compile and run, choose name 'Debug', choose unlocked modes '2', choose mode '2', continue? 'n', choose mode '2', continue? 'y', make a valid choice 'y'.