Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 我的Python迷你游戏。这让我很困惑:-(_Python 2.7 - Fatal编程技术网

Python 2.7 我的Python迷你游戏。这让我很困惑:-(

Python 2.7 我的Python迷你游戏。这让我很困惑:-(,python-2.7,Python 2.7,我正在使用Python2.7并学习PythonTheHardWay一书 然而,我有一些创造性的想法,我想在这个练习中做一些改变: 所以我写了自己的代码: from sys import exit times_in_exchange_path = 0 asked_for_help = 0 def exchange_path(): print "Hola! You are about to get to the very dangerous point, kid!"

我正在使用Python2.7并学习PythonTheHardWay一书

然而,我有一些创造性的想法,我想在这个练习中做一些改变:

所以我写了自己的代码:

from sys import exit

times_in_exchange_path = 0
asked_for_help = 0

def exchange_path():
    print "Hola! You are about to get to the very dangerous point, kid!"
    print "You are independent from now. With luck..."

    action = raw_input('...')

    if 'what' in action:
        print 'I said you are on your own now. No one is gonna help you',
        print "with anything..."
        exchange_path()
    elif 'creepy' in action:
        print "I found it creepy, too. Kid. Take me out of here with you."
        print "Is that OK?"
        haunted = False

        decision = raw_input()

        if decision == 'agree':
            print "Finally. I'm free!"
            haunted = True
            times_in_exchange_path = times_in_exchange_path + 1
            start()
        else:
            dead("I will make you know what pain really is!!?! >:(")

    else:
        dead("A shadow barged into you, and you broke your neck.")

def gold_room():
    print "This room is full of gold.  How much do you take?"

    choice = raw_input("> ")
    try:
        how_much = int(choice)
    except:
        dead("Man, learn to type a number.")

    if how_much < 50:
        print "Nice, you're not greedy, you win!"

        if haunted:
            print "\nHowever... I can't just let you get away easily like this."
            print '"Why??" - I asked'
            print "You belong to the gold mine now!!"
            print '"What do you mean??" - I replied in fear'
            print "Power requires sacrifice. Muhahahhaha!"
            dead(" ")
        else:
            exit(0)

    else:
        dead("You greedy bastard! It's a trap for you!")

def bear_room():
    print "There is a bear here."
    print "The bear has a bunch of honey."
    print "The fat bear is in front of another door."
    print "How are you going to move the bear?\n"
    bear_moved = False

    if haunted:
        print "...Wait. The bear suddenly shouts at you, break the window",
        print "and runs far far away."
        print "Let's go to the door, kid...\n\n"
        gold_room()
    else:
        while True:
            choice = raw_input("> ")

            if choice == "take honey":
                dead("The bear looks at you then slaps your face off.")
            elif choice == "taunt bear" and not bear_moved:
                print "The bear has moved from the door. You can go through",
                print "it now."
                bear_moved = True
            elif choice == "taunt bear" and bear_moved:
                dead("The bear gets pissed off and chews your leg off.")
            elif choice == "open door" and bear_moved:
                gold_room()
            else:
                print "I got no idea what that means."


def cthulhu_room():
    print "Here you see the great evil Cthulhu."
    print "He, it, whatever stares at you and goes insane."
    print "Do you flee for your life or he eats your head?"
    cthulhu_dead = False

    choice = raw_input("> ")

    if cthulhu_dead:
        print "There's nothing here, kid. Except of the light and a dead body."
        print "Let's go!\n...\n\n"
        start()
    else:

        if "flee" in choice:
            start()
        elif "head" in choice:
            dead("Well that was tasty!")
        elif "fight" in choice:

            if haunted:
                print "With mysterious distraction, the Cthulhu turns his back",
                print "at you. You use the shiny magical spear nearby to kill",
                print "the monster."
                cthulhu_dead = True
                print "\n...\n"
                start()
            else:
                dead("Awesome tasty head you have!")

        else:
            cthulhu_room()


def dead(why):
    print why, "\n\nGood job! You died."
    exit(0)


def first_assist():
    asked_for_help += 1

    if asked_for_help in range(4, 6):
        print "Stop! You are attracting them!"
        print "A portal shows up and you are walking into it...\n\n"
    elif asked_for_help > 5:
        dead("Those demons rise from the ground. And burn you down!")
    else:
        print "Hi! I am your beautiful assistance."
        print "As you got lost in here, I will show you the way."
        print "You have to know that all three paths are dangerous, and there",
        print "is no way out of here.\nSo, try to survive with glory."
        print '"left", "right" and "front" are the first three spells you use.'
        print "There's a mighty power near here. Have a pure heart, and know",
        print "that you will never have to touch that evil power..."
        print "Good luck from here... Be wise."
    start()


def start():
    print "You are in a dark room."
    print "There is a door to your right and left."
    print 'And a middle path, full of darkness, in front of you.'
    print "Which one do you dare to take?"

    choice = raw_input("> ")

    if choice == "left":
        bear_room()
    elif choice == "right":
        cthulhu_room()
    elif choice == "front":

        if times_in_exchange_path == 0:
            exchange_path()
        else:
            print "You feel something stopping you from entering this place. \n"
            start()

    elif "help" in choice:
        first_assist()
    else:
        dead("You stumble around the room until you starve.")


start()
从系统导入退出
交换路径中的次数=0
请求\u帮助=0
def交换路径():
打印“你好!你马上就要到危险的地步了,孩子!”
打印“你现在独立了,运气好…”
动作=原始输入(“…”)
如果“什么”在起作用:
打印“我说你现在只能靠自己了,没人会帮你”,
打印“任何东西…”
交换路径()
elif“令人毛骨悚然”的行动:
打印“我也觉得很恐怖,孩子,带我离开这里。”
打印“可以吗?”
闹鬼
决策=原始输入()
如果决策=‘同意’:
打印“终于,我自由了!”
闹鬼
交换路径中的次数=交换路径中的次数+1
开始()
其他:
死亡(“我会让你知道痛苦到底是什么!!?!>:()
其他:
死亡(“一个影子闯入你,你摔断了脖子。”)
def gold_房间():
打印“这间屋子里满是金子,你要多少钱?”
选择=原始输入(“>”)
尝试:
多少=整数(选择)
除:
死亡(“伙计,学着打一个数字。”)
如果多少小于50:
打印“很好,你不贪婪,你赢了!”
如果闹鬼:
打印“\n无论如何……我不能就这样让你轻易逃脱。”
打印“为什么?”——我问
打印“你现在属于金矿!!”
打印“你什么意思?”——我害怕地回答
打印“力量需要牺牲,哈哈哈!”
死亡(“”)
其他:
出口(0)
其他:
死(“你这个贪婪的混蛋!这是你的陷阱!”)
def bear_房间():
打印“这里有一只熊。”
打印“熊有一束蜂蜜。”
打印“肥熊在另一扇门前。”
打印“如何移动熊?\n”
bear_moved=假
如果闹鬼:
打印“…等等,熊突然对你喊叫,打破窗户”,
打印“并跑得很远。”
打印“孩子,我们去开门吧…\n\n”
黄金屋
其他:
尽管如此:
选择=原始输入(“>”)
如果选择==“吃蜂蜜”:
死了(“熊看着你,然后打你的脸。”)
elif choice==“嘲弄熊”和不移动熊:
打印“熊已经离开了门,你可以通过”,
打印“现在”
bear_moved=True
elif choice==“嘲弄熊”和熊_移动:
死了(“熊生气了,把你的腿咬掉了。”)
elif choice==“打开门”和bear_移动:
黄金屋
其他:
打印“我不知道那是什么意思。”
def CTHULU_房间():
打印“这里你看到了邪恶的克图鲁。”
打印“他,它,任何盯着你看并发疯的东西。”
打印“你是逃命还是他吃了你的头?”
cthulhu_dead=错误
选择=原始输入(“>”)
如果cthulhu_死亡:
打印“这里什么都没有,孩子。除了光和尸体。”
打印“走吧!\n…”。\n\n
开始()
其他:
如果选择“逃离”:
开始()
选择中的elif“头”:
死了(“嗯,那太好吃了!”)
选择中的elif“战斗”:
如果闹鬼:
印刷品“带着神秘的分心,克丘鲁人转过身来”,
打印“对着你,你用附近闪亮的魔法长矛杀人”,
打印“怪物”
cthulhu_dead=正确
打印“\n…”
开始()
其他:
死了(“你的脑袋真好吃!”)
其他:
cthulhu_室()
def死亡(原因):
打印原因,“\n\n干得好!你死了。”
出口(0)
def first_assist():
请求\u帮助+=1
如果在范围(4,6)内向您寻求帮助:
打印“停止!你正在吸引他们!”
打印“出现一个门户,您正在进入它…\n\n”
elif向_寻求帮助>5:
死亡(“那些恶魔从地上爬起来,把你烧死了!”)
其他:
打印“嗨!我是你美丽的助手。”
打印“当你在这里迷路时,我会给你指路。”
打印“你必须知道所有三条路径都是危险的,并且有”,
“打印”是不可能离开这里的。\n所以,试着在荣耀中生存
打印“左”、“右”和“前”是您使用的前三个拼写
打印“附近有强大的力量。拥有一颗纯洁的心,并且知道”,
打印“你永远不会接触到邪恶的力量…”
打印“从这里祝你好运……明智点。”
开始()
def start():
打印“你在一个黑暗的房间里。”
打印“你的左右两侧各有一扇门。”
打印“和一条中间的道路,充满黑暗,在你面前。”
打印“你敢拿哪一个?”
选择=原始输入(“>”)
如果选项==“左”:
熊屋
elif choice==“右”:
cthulhu_室()
elif选项==“正面”:
如果交换路径中的次数=0:
交换路径()
其他:
打印“您觉得有什么东西阻止您进入这个地方。\n”
开始()
elif“帮助”选项:
第一次协助
其他:
死亡(“你在房间里跌跌撞撞,直到饿死。”)
开始()
但是由于某些原因,我的代码不能正常工作

它首先在屏幕上打印出如下行:

你在一个黑暗的房间里

在你的左右两边都有一扇门

一条中间的小路,充满了黑暗

你敢拿哪一个

然后,无论我输入什么,程序都会出错。我不知道为什么

请帮我修理这个游戏

非常感谢。:-)
Traceback (most recent call last):
  File "game.py", line 174, in <module>
    start()
  File "game.py", line 159, in start
    cthulhu_room()
  File "game.py", line 120, in cthulhu_room
    cthulhu_room()
  File "game.py", line 104, in cthulhu_room
    start()
  File "game.py", line 159, in start
    cthulhu_room()
  File "game.py", line 104, in cthulhu_room
    start()
  File "game.py", line 157, in start
    bear_room()
  File "game.py", line 66, in bear_room
    if haunted:
NameError: global name 'haunted' is not defined
...
asked_for_help = 0
haunted = False    # Or False, whatever you choose

def exchange_path():
    global haunted
    ...
    if haunted:
        ...