艰苦地学习Python ex35

艰苦地学习Python ex35,python,Python,我在做练习35(分支和函数),但我输入的代码不起作用,在寻找错误一段时间后,我试图从书中复制粘贴,但没有起作用 代码如下: from sys import exit def gold_room(): print "This room is full of gold. How much do you take?" next = raw_input("> ") if "0" in next or "1" in next: how_much = in

我在做练习35(分支和函数),但我输入的代码不起作用,在寻找错误一段时间后,我试图从书中复制粘贴,但没有起作用

代码如下:

from sys import exit

def gold_room():

    print "This room is full of gold. How much do you take?"

    next = raw_input("> ")
    if "0" in next or "1" in next:
        how_much = int(next)
    else:
        dead("Man, learn to type a number.")

    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")


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?"
    bear_moved = False

while True:

    next = raw_input("> ")

    if next == "take honey":
        dead("The bear looks at you then slaps your face off.")
    elif next == "taunt bear" and not bear_moved:
        print "The bear has moved from the door. You can go through it now."
        bear_moved = True
    elif next == "taunt bear" and bear_moved:
        dead("The bear gets pissed off and chews your leg off.")
    elif next == "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 you go insane."
    print "Do you flee for your life or eat your head?"

    next = raw_input("> ")

    if "flee" in next:
        start()
    elif "head" in next:
        dead("Well that was tasty!")
    else:
        cthulhu_room()


def dead(why):

    print why, "Good job!"
    exit(0)

def start():

    print "You are in a dark room."
    print "There is a door to your right and left."
    print "Which one do you take?"

    next = raw_input("> ")

    if next == "left":
        bear_room()
    elif next == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve.")



start()
从系统导入退出
def gold_房间():
打印“这间屋子里满是金子,你要多少钱?”
下一步=原始输入(“>”)
如果下一个中的“0”或下一个中的“1”:
多少=整数(下一个)
其他:
死亡(“伙计,学着打一个数字。”)
如果多少小于50:
打印“很好,你不贪婪,你赢了!”
出口(0)
其他:
死了(“你这个贪婪的混蛋!”)
def bear_房间():
打印“这里有一只熊。”
打印“熊有一束蜂蜜。”
打印“肥熊在另一扇门前。”
打印“你打算如何移动熊?”
bear_moved=假
尽管如此:
下一步=原始输入(“>”)
如果下一步==“吃蜂蜜”:
死了(“熊看着你,然后打你的脸。”)
elif next==“嘲弄熊”且未移动熊:
打印“熊已经离开了门。你现在可以穿过它了。”
bear_moved=True
elif next==“嘲弄熊”和熊_移动:
死了(“熊生气了,把你的腿咬掉了。”)
elif next==“开门”和bear_移动:
黄金屋
其他:
打印“我不知道那是什么意思。”
def CTHULU_房间():
打印“这里你看到了邪恶的克图鲁。”
打印“他,它,任何盯着你看的东西,你都会发疯。”
打印“你是逃命还是吃脑袋?”
下一步=原始输入(“>”)
如果在下一步中“逃离”:
开始()
下一页中的elif“head”:
死了(“嗯,那太好吃了!”)
其他:
cthulhu_室()
def死亡(原因):
打印为什么,“干得好!”
出口(0)
def start():
打印“你在一个黑暗的房间里。”
打印“你的左右两侧各有一扇门。”
打印“你选哪一个?”
下一步=原始输入(“>”)
如果下一步==“左”:
熊屋
elif next==“右”:
cthulhu_室()
其他:
死亡(“你在房间里跌跌撞撞,直到饿死。”)
开始()

当我运行它时,它会显示“>”,这是while循环,但当我键入“takehoney”时,它会说dead没有定义,我不理解。(如果我用英语写错了,很抱歉)

只需将
dead
的函数定义移到文件顶部(调用它的位置上方)

从系统导入退出
def死亡(原因):
打印为什么,“干得好!”
出口(0)
def gold_房间():
打印“这间屋子里满是金子,你要多少钱?”
下一步=原始输入(“>”)
如果下一个中的“0”或下一个中的“1”:
多少=整数(下一个)
其他:
死亡(“伙计,学着打一个数字。”)
如果多少小于50:
打印“很好,你不贪婪,你赢了!”
出口(0)
其他:
死了(“你这个贪婪的混蛋!”)
def bear_房间():
打印“这里有一只熊。”
打印“熊有一束蜂蜜。”
打印“肥熊在另一扇门前。”
打印“你打算如何移动熊?”
bear_moved=假
尽管如此:
下一步=原始输入(“>”)
如果下一步==“吃蜂蜜”:
死了(“熊看着你,然后打你的脸。”)
elif next==“嘲弄熊”且未移动熊:
打印“熊已经离开了门。你现在可以穿过它了。”
bear_moved=True
elif next==“嘲弄熊”和熊_移动:
死了(“熊生气了,把你的腿咬掉了。”)
elif next==“开门”和bear_移动:
黄金屋
其他:
打印“我不知道那是什么意思。”
def CTHULU_房间():
打印“这里你看到了邪恶的克图鲁。”
打印“他,它,任何盯着你看的东西,你都会发疯。”
打印“你是逃命还是吃脑袋?”
下一步=原始输入(“>”)
如果在下一步中“逃离”:
开始()
下一页中的elif“head”:
死了(“嗯,那太好吃了!”)
其他:
cthulhu_室()
def start():
打印“你在一个黑暗的房间里。”
打印“你的左右两侧各有一扇门。”
打印“你选哪一个?”
下一步=原始输入(“>”)
如果下一步==“左”:
熊屋
elif next==“右”:
cthulhu_室()
其他:
死亡(“你在房间里跌跌撞撞,直到饿死。”)
开始()

python中的缩进非常重要,看起来您没有缩进
while
语句,整个内容在
while
下,包括
while
本身应该属于
def bear_room()

bear\u room
应该是这样的,否则在
start()调用之前运行
while
循环:

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?"
    bear_moved = False

    while True:

        next = raw_input("> ")

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

很可能代码中有空格错误。这意味着您忘记了某个地方的选项卡,因此您对
无效的
(函数)的定义永远无法达到。当它被调用时(
dead(“…”
),它没有被定义,脚本停止

下面是您的代码的外观:

from sys import exit

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

    next = raw_input("> ")
    if "0" in next or "1" in next:
        how_much = int(next)
    else:
        dead("Man, learn to type a number.")

    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")

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?"
    bear_moved = False
    while True:
        next = raw_input("> ")

        if next == "take honey":
            dead("The bear looks at you then slaps your face off.")
        elif next == "taunt bear" and not bear_moved:
            print "The bear has moved from the door. You can go through it now."
            bear_moved = True
        elif next == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        elif next == "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 you go insane."
    print "Do you flee for your life or eat your head?"

    next = raw_input("> ")

    if "flee" in next:
        start()
    elif "head" in next:
        dead("Well that was tasty!")
    else:
        cthulhu_room()

def dead(why):
    print why, "Good job!"
    exit(0)

def start():
    print "You are in a dark room."
    print "There is a door to your right and left."
    print "Which one do you take?"

    next = raw_input("> ")

    if next == "left":
        bear_room()
    elif next == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve.")

start()
从系统导入退出
def gold_房间():
打印“这间屋子里满是金子,你要多少钱?”
下一步=原始输入(“>”)
如果下一个中的“0”或下一个中的“1”:
多少=整数(下一个)
其他:
死亡(“伙计,学着打一个数字。”)
如果多少小于50:
打印“很好,你不贪婪,你赢了!”
出口(0)
其他:
死了(“你这个贪婪的混蛋!”)
def bear_房间():
打印“这里有一只熊。”
打印“熊有一束蜂蜜。”
打印“肥熊在另一扇门前。”
打印“你打算如何移动熊?”
bear_moved=假
尽管如此:
下一步=原始输入(“>”)
如果下一步==“吃蜂蜜”:
死了(“熊看着你,然后打你的脸。”)
elif next==“嘲弄熊”且未移动熊:
打印“熊已经离开了门。你现在可以穿过它了。”
bear_moved=True
elif next==“嘲弄熊”和熊_移动:
死了(“熊被激怒了,然后
from sys import exit

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

    next = raw_input("> ")
    if "0" in next or "1" in next:
        how_much = int(next)
    else:
        dead("Man, learn to type a number.")

    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")

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?"
    bear_moved = False
    while True:
        next = raw_input("> ")

        if next == "take honey":
            dead("The bear looks at you then slaps your face off.")
        elif next == "taunt bear" and not bear_moved:
            print "The bear has moved from the door. You can go through it now."
            bear_moved = True
        elif next == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        elif next == "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 you go insane."
    print "Do you flee for your life or eat your head?"

    next = raw_input("> ")

    if "flee" in next:
        start()
    elif "head" in next:
        dead("Well that was tasty!")
    else:
        cthulhu_room()

def dead(why):
    print why, "Good job!"
    exit(0)

def start():
    print "You are in a dark room."
    print "There is a door to your right and left."
    print "Which one do you take?"

    next = raw_input("> ")

    if next == "left":
        bear_room()
    elif next == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve.")

start()