2017-10-26 8 views
0

私は現在、Pythonでコードを作成する方法を学んでいます。私はこのコードを勉強しています(Pythonを学ぶことは難しいです。 )。Pythonはどのような順序でコードを読んでいますか? (サンプルコード含む)

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() 

は、私はいつもPythonが左から右へとダウンまでが、上記のコードでからコードを読み込むことを考えている、それは からプログラムの実行を開始デフ()開始:あなたは暗闇の中である 印刷」ルーム。" 誰かが私のためにこれをクリアすることができれば、それは大きな助けになるでしょう、私はPythonを作ることを理解していません。ありがとうございます。

答えて

1

私はいつもPythonが左から右にコードを読み取り、

アップからダウンには、左から右へ、はい、トップダウンを読み取ります。と思っています

def gold_room():のみ機能gold_roomを定義し、それがない実行それをしません。下のgold_room()がなければ、決して実行されません。 start()と同じです。

+0

Omg私はちょうど感謝を得ました:D ...しかし、清潔さのために、def start()を置く方が良いでしょうか?物事の「順序」や「秩序」のようなものを持っているだけですか?それともプログラマが大変なことですか?もしそうなら、なぜですか?編集:私はソースコードの初めに入れてみました、それはうまく動作するようです。 –

+0

@エリック:nah、それは価値がありません(実際には、呼び出し順序に従ってファイル内に関数を配置する)。ほとんどの場合、注文がないことがほとんどです!私は、最も重要な部分をファイルの先頭に置く傾向がありますが、それはそれです。 –

+0

、もう一度、ありがとう:) –

関連する問題