Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3游戏语法错误:';返回';外部功能';艰苦地学习Python 3';_Python_Python 3.x - Fatal编程技术网

Python 3游戏语法错误:';返回';外部功能';艰苦地学习Python 3';

Python 3游戏语法错误:';返回';外部功能';艰苦地学习Python 3';,python,python-3.x,Python,Python 3.x,我目前正在写《艰苦学习Python 3》一书,我被困在一个练习中,你应该在那里创建一个游戏 我遇到了一个错误,上面写着: 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

我目前正在写《艰苦学习Python 3》一书,我被困在一个练习中,你应该在那里创建一个游戏

我遇到了一个错误,上面写着:

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()
文件“ex45.py”,第83行
返回“死亡”
^
SyntaxError:函数外部的“return”。
从随机导入randint
从系统导入退出
从textwrap导入dedent
类场景(对象):
def输入(自我):
打印(dedent(“”)
你进了一扇门
你被传送到另一个世界!
"""))
类引擎(对象):
定义初始(自我,场景贴图):
self.scene\u map=scene\u map
def播放(自我):
当前场景=self.scene\u map.opening\u scene()
最后一个场景=self.scene\u map.next\u场景('finished'))
当前_场景时!=最后一幕:
下一个场景名称=当前场景。输入()
当前场景=self.scene\u map.next\u场景(next\u场景名称)
当前场景。输入()
课堂结束(场景):
def输入(自我):
打印(“你赢了!干得好。”)
返回“已完成”
集体死亡(现场):
俏皮话=[
“你死定了,普塔!”
“你死了,儿子。”
“该死的,你真烂!”
]
def输入(自我):
打印(死亡俏皮话[randint(0,len(self.quips)-1)])
出口(1)
Angalania级(场景):
打印(dedent(“”)
欢迎来到“Angalania”,这是一个充满活力的世界
美丽的生物和植物,但要小心!
还有很多怪物!
你将进入几所房子寻找你的财富
或者你的痛苦!
祝你好运!
你在美丽的安达里诺村
如果你有两个选择,要么你进入花园,要么
年轻漂亮的女人,她用熊和
食物,但在另一边等着还有一个花园,不是吗
美丽但不丑陋有一位老妇人在警告
你…“别进来,小男孩,过来吧。”那个年轻人说
女人是一个女巫,她把自己变年轻了,这样她就可以
引诱像你这样的年轻男孩,吃掉他们的肉,留下来
年轻的。。。
在1号花园和2号花园之间选择。
"""))
选择=输入(“>”)
如果选择='1'或选择='1':
打印(dedent(“”)
不错的选择年轻人,年轻女子不是女巫
她是一位美丽纯洁的公主,她爱你
直到死亡,用食物和饮料喂养你。。
"""))
elif choice=='2'或choice=='2':
打印(dedent(“”)
该死的!
你应该相信你的直觉。。。
老妇人就是那个女巫。。。
"""))
返回“死亡”
其他:
打印(“不计算!”)
返回“angalania”
第二课堂(现场):
def输入(自我):
打印(dedent(“”)
在你进入真正的公主花园之后,
老巫婆吃醋了,把她的小魔鬼送到了医院
她的房子。
他们带走了公主,让你猜一猜
游戏下面有3个杯子,里面有一个球。
你必须在1-3之间选择正确的杯子。你有3个
机会。。。
洗牌。。。。。。。。。。
"""))
cup=f“{randint(1,3)}{randint(1,3)}{randint(1,3)}”
猜测=输入(“>”)
猜测=3
猜猜看!=杯子和猜测<3:
打印(“错误!”)
猜测+=1
猜测=输入(“>”)
如果猜测==杯子:
打印(dedent(“”)
干得好!公主被释放了,你报复了
更多的食物和饮料!
"""))
返回“城堡”
其他:
打印(dedent(“”)
你第三次选择了错误的答案!
你听到“aaaaaaaaah”,看到公主的头
滚下楼梯。
现在小魔鬼把注意力转向你。。。
"""))
返回“死亡”
课堂(场景):
def输入(自我):
打印(dedent(“”)
你已经进入最后一幕了!
在濒死体验之后
你决定结束这一切
杀了老巫婆!
城堡有5扇门!
你们开哪一家?
"""))
好的门=randint(1,5)
猜测=输入(“[door#]>”)
如果int(猜测)!=好的门:
打印(dedent(“”)
你进错房间了!
到处都是蛇,你呢
被咬成虫子。。。
"""))
返回“死亡”
其他:
打印(dedent(“”)
干得好!你找到女巫了!
你一刀一刀地砍,
直到没有更多的刀砍!
现在你和公主永远幸福地生活着,
作为女王和国王!
"""))
返回“已完成”
类映射(对象):
场景={
“angalania”:angalania(),
“dieorlive”:dieorlive(),
“thecastle”:thecastle(),
“死亡”:死亡(),
“已完成”:已完成(),
}
def_uuinit(self,start_场景):
self.start\u场景=start\u场景
定义下一个场景(自身、场景名称):
val=Map.scenes.get(场景名称)
返回值
def打开_场景(自身):
返回self.next_场景(self.start_场景)
a_map=map('angalania')
游戏=引擎(地图)
游戏

错误为您提供行号,因此请查看它,并使用显示缩进级别的编辑器

  • Angalania类
    根本没有任何功能。还有一些回报


  • 然后您有一个if语句,并且
    返回没有缩进的'death'
    elif choice='2'或choice='2':。。。返回“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?
                """))
    
        #------- indentation here is outside of a function  ------
        good_door = randint(1, 5)
        guess = input("[door #]> ")