I am trying to incorporate the while-loop name game from here into my game for ex45. As part of this, I’ve been attempting to return scenes from within the while loop. Previously, I had defined a function outside name_game()
and called that from within the loop, but had the same errors.
The traceback that is triggered on both a “failed” if or a “successful” else is:
Traceback (most recent call last):
File "palace.py", line 465, in <module>
File "palace.py", line 28, in play
next_scene_name = current_scene.enter()
AttributeError: 'NoneType' object has no attribute 'enter'
I’m not sure what would be the ‘NoneType’ in this case.
The section that fails is:
class Ball(Scene):
def enter(self):
print(dedent('''
If you succeed you will get the success message from the else statement and then a traceback error on the return function. If you fail you will get the fail statement from the if statement and a traceback on the return function.
'''))
def name_game():
name = 'Shame'
guess = input("What is the magician's name?: ")
pos = 0
while guess != name and pos < len(name):
print("A simple task, yet you fail. Try again. A clue: letter ", end='')
print(pos + 1, "is", name[pos] + ". ", end='')
guess = input("Guess again: ")
pos = pos + 1
if pos == len(name) and name != guess:
print("Too bad, you couldn't get it. The name was", name + ".")
return 'ghouls'
else:
print("\nYou have succeeded!")
return 'woman'
name_game()
The lines from the traceback are a direct copy of the Engine given in ex45:
class Engine(object):
def __init__(self, scene_map):
self.scene_map = scene_map
def play(self):
current_scene = self.scene_map.opening_scene()
last_scene = self.scene_map.next_scene("finished")
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
#be sure to print out the last Scene
current_scene.enter()
Is there another way to return the next scene within the while loop without using a return? Does this require revising the main engine? Or making a second engine?