python - Python 3 游戏 SyntaxError: 'return' 外部函数。“艰难地学习 Python 3”
问题描述
我目前正在使用“Learn Python 3 the hard way”一书,并且我陷入了应该创建游戏的练习中。
我遇到了一个错误,上面写着:
File "ex45.py", line 83
return 'death'
^
SyntaxError: 'return' outside function.
from random import randint
from sys import exit
from textwrap import dedent
class Scene(object):
def enter(self):
print(dedent("""
You enter a door
and you are teleported to a whole nother world!
"""))
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)
current_scene.enter()
class Finished(Scene):
def enter(self):
print("You won! Good job.")
return 'finished'
class Death(Scene):
quips = [
"You're dead puta!"
"You died son."
"God damn it, YOU SUCK!"
]
def enter(self):
print(Death.quips[randint(0, len(self.quips)-1)])
exit(1)
class Angalania(Scene):
print(dedent("""
Welcome to 'Angalania', the world that is full of
beautiful creatures and plants, but beware!
There also alot of monsters!
You will be entering several houses to seek your fortune
OR YOUR PAIN!
Godspeed!
You're in the beautiful village of 'Andarino'
where you have 2 choices either you enter garden #1 of the
young and beautiful woman who is tempting you with bear and
food, but wait on the other side there is another garden, not
beautiful but not ugly there is an old woman that is warning
you...'Do not enter young boy, come here instead.' That young
woman is a witch who has turned herself young so she can
tempt young boys like you and eat their flesh to stay
young...
Choose between garden #1 or #2.
"""))
choice = input('> ')
if choice == '1' or choice == '#1':
print(dedent("""
Good choice young man, the young woman wasn't a witch
she was a beautiful innocent princess who loves you
til' death, and feeds you with food and beverage..
"""))
elif choice == '2' or choice == '#2':
print(dedent("""
Stupid fuck!
You should have trusted your instincts...
The old woman was the witch...
"""))
return 'death'
else:
print("DOES NOT COMPUTE!")
return 'angalania'
class DieOrLive(Scene):
def enter(self):
print(dedent("""
After you entered the true princesses garden,
the old witch got jealous and sent her little devils to
her house.
they took the princess and made you guess play a guessing
game underneath 3 cups, there is one ball.
you have to choose the right cup between 1-3. You have 3
chances...
Shuffling..........
"""))
cup = f"{randint(1,3)}{randint(1,3)}{randint(1,3)}"
guess = input("> ")
guesses = 3
while guess != cup and guesses < 3:
print("WRONG!")
guesses += 1
guess = input("> ")
if guess == cup:
print(dedent("""
Good Job! The princess is released and you get even
more food and beverage!
"""))
return 'the_castle'
else:
print(dedent("""
You choose the wrong answer for the third time!
You hear 'AAAAAAAAAAH' and see the princess's head
roll down the stairs.
Now the little devils turn their attention to you...
"""))
return 'death'
class TheCastle(Scene):
def enter(self):
print(dedent("""
You have entered the last scene!
After the near-death experience
you decided to end this bullshit
and kill the old witch!
At the castle there are 5 doors!
Which one do you open?
"""))
good_door = randint(1, 5)
guess = input("[door #]> ")
if int(guess) != good_door:
print(dedent("""
You enter the wrong room!
It's full of snakes and you
get bitten and turn into a worm...
"""))
return 'death'
else:
print(dedent("""
Good Job! You have found the witch!
And you slash and you slash and you slash,
until there is nothing more to slash!
Now you and the princess live forever happily,
As Queen and King!
"""))
return 'finished'
class Map(object):
scenes = {
'angalania': Angalania(),
'dieorlive': DieOrLive(),
'thecastle': TheCastle(),
'death': Death(),
'finished': Finished(),
}
def __init(self, start_scene):
self.start_scene = start_scene
def next_scene(self, scene_name):
val = Map.scenes.get(scene_name)
return val
def opening_scene(self):
return self.next_scene(self.start_scene)
a_map = Map('angalania')
a_game = Engine(a_map)
a_game.play()
解决方案
错误为您提供行号,因此请查看它,并使用显示缩进级别的编辑器
class Angalania
根本没有任何功能。还有一些回报。
class TheCastle(Scene):
def enter(self):
print(dedent("""
You have entered the last scene!
After the near-death experience
you decided to end this bullshit
and kill the old witch!
At the castle there are 5 doors!
Which one do you open?
"""))
#------- indentation here is outside of a function ------
good_door = randint(1, 5)
guess = input("[door #]> ")
然后你有一个 if 语句并且return 'death'
没有缩进
推荐阅读
- python - 如何使用像素值/边框值将图像填充到特定大小,例如 720*576?
- c# - Visual Studio 侧面的选项卡
- python-3.x - 在 tkinter 中生成动态数量的复选框
- java - 无法使用 selenium webdriver 完全剪切宽横幅图像,仅剪切图像的一部分
- windows-subsystem-for-linux - apt-get update 和 apt-get upgrade 以某种方式杀死了 Linux 的 Windows 子系统
- mongodb - WebFlux 在这样的架构中会有瓶颈吗?
- sql - 在 MS SQL 中加入两个 SQL 查询
- maven - 我可以使用一个 Maven 命令在多个设备上运行相同的黄瓜套件吗?
- angular - 为什么 canActivate 和 canActivateChild 属性是无类型的?
- .net - 是否可以在项目 .Net framework 4.5 中引用 IdentityModel 库?