Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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以什么顺序读取代码?(包括示例代码)_Python - Fatal编程技术网

Python以什么顺序读取代码?(包括示例代码)

Python以什么顺序读取代码?(包括示例代码),python,Python,我目前正在学习如何用Python编写代码,我在我学习的书中偶然发现了这段代码(艰难地学习Python[我不推荐任何刚开始编写代码的人]) 从系统导入退出 def gold_房间(): 打印“这间屋子里满是金子,你要多少钱?” 下一步=原始输入(“>”) 如果下一个中的“0”或下一个中的“1”: 多少=整数(下一个) 其他: 死亡(“伙计,学着打一个数字。”) 如果多少小于50: 打印“很好,你不贪婪,你赢了!” 出口(0) 其他: 死了(“你这个贪婪的混蛋!”) def bear_房间(): 打

我目前正在学习如何用Python编写代码,我在我学习的书中偶然发现了这段代码(艰难地学习Python[我不推荐任何刚开始编写代码的人])

从系统导入退出
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_室()
其他:
死亡(“你在房间里跌跌撞撞,直到饿死。”)
开始()
我一直认为Python从左到右、从上到下读取代码,但在上面的代码中,它从 def start(): 打印“你在一个黑暗的房间里。” 我不明白是什么让Python这么做的,如果有人能帮我弄清楚这一点,那将是非常有帮助的。先谢谢你

我一直认为Python从左到右、从上到下读取代码

阅读,是的,自上而下,从左到右


def gold_room():
仅定义
功能
gold_room
,不运行它。如果没有下面的
gold\u room()
,它将永远不会被执行。与
start()

天哪,我刚收到了谢谢:D…但是为了“清洁”,把def start():放在顶部不是更好吗?只是为了有一种“顺序”或“顺序”的东西?或者这是程序员经常做的事情?如果是,为什么?编辑:我试着把它放在源代码的开头,它似乎可以运行fine@EricAhn:不,大多数情况下不值得(根据函数的“调用顺序”在文件中实际排列函数)。主要是因为经常没有订单!在我看来,我倾向于将最重要的部分放在文件的开头,但仅此而已。@EricAhn注意到代码的顺序很有趣。在Robert C Martin的《干净的代码》第三章中,它说“我们希望能够像阅读一组to段落一样阅读程序,每个段落都描述了当前的抽象层次,并引用了下一层次的后续段落……”如果你能做到这一点,那就太好了。但不要把它当作教条。总结:
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 you.")
        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 legs 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()