I'm building a game as part of a tutorial on learning code.
The following class has a while loop that should return either 'finished' or, leave the loop and return 'death' (These are dict entries that run the game) but instead does not even seem to run. I'm looking at the while loop after def guess:
The loop is meant to ask the user to guess a number between 1 and three. If they guess wrong more than three times they "lose" and 'death' is returned, else 'finished'.
But, when I play the game I am not even prompted to enter a number, instead "Too many failed guesses, you lose!" is printed, even if guesses is 0.
class Smaug(Scene):
def enter(self):
print "Smaug is a terrifying huge fire breathing dragon, but you must get the Arkenstone from him for Thorin"
print "In Smaug's cave, the Lonely Mountain, Smaug notices your presence and challenges you to a game"
print "He says \"Guess a number between 1 and 3\""
smaugNum = random.randint(1, 3)
print "Smaugs number cheat:", smaugNum
guesses = 0
def guess():
while guesses < 4:
print "Guess a number between 1 and 3"
numb = raw_input("> ")
if numb == smaugNum:
print "Well done! You win."
Player.BilbosStuff.append('arkenstone')
print "Now Bilbo has", Player.BilbosStuff
return 'finished'
else:
print "You lose!"
guesses += 1
guess()
print "Too many failed guesses, you lose!"
return 'death'
Looking at the nesting of the code blocks, is it that when 'finished' is returned in the while loop, does it also, automatically, get returned as part of the wider class? Put another way, if numb == smaugNum then I need Smaug class to return finished.
guessis never called in this code...1and2, as the end of the range is excluded. Alsoraw_inputgives a str, not a number, so it will never test equal.