2016-09-06 5 views
0

Zed ShawのLPTHWの35を実行する機能を追加したかったのです。このスクリプトはクラッシュすることなく実行され、プレイヤーは以前の部屋に戻ってきます。しかし、bear_roomでは、プレイヤーが得ることができる唯一の方法は、bear_movedブール値をTrueに切り替えることです。なぜクマ(ブール値)はその位置にありませんか?

もしプレイヤーがこれを行い、その後に出発部屋に戻ると、bear_movedブール値が真の位置にとどまっていたことが意図されていました。つまり、クマはまだ再入場時にドアから離れることになります。

スクリプトを実行すると、これは起こりません。最初にbear_roomに入ると、私はクマを叫び、ドアから離れるようにします。私は部屋から戻って出発部屋に戻ります。私がクマの部屋に戻ったとき、クマは神秘的にドアの前にその肥満の自己を再現しました。

私は、この目的のために関数の外にbear_movedブール値を配置しました。これは、プログラムに余分な機能を与えるために思いついた唯一のものでした。

私はbear_roomを終了して再入力すると、なぜクマは動いていないのですか?私が目指している機能をどのように達成できますか?グローバル機能が固定で

from sys import exit 
bear_moved = False 

def gold_room(): 
    print "This room is full of gold. How much do you take?" 
    next = raw_input("> ") 
    if next.isdigit(): 
     if int(next) > 101: 
      dead("You greedy bastard!") 
     elif int(next) < 101: 
      print "Nice, you're not greedy! You win!" 
      exit(0) 
     else: 
      pass 
    elif 'back' in next or 'backtrack' in next: 
     bear_room(bear_moved) 
    else: 
     print "Type a number!" 
     gold_room() 

def bear_room(bear_moved): 
    if bear_moved == False: 
     print "There's a bear in 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?" 
    elif bear_moved == True: 
     print "The bear has moved away from the door." 
    else: 
     pass 

    next = raw_input("> ") 
    if 'honey' in next: 
     dead("The looks at you and then slaps your face off.") 
    elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == False: 
     bear_moved = True 
     bear_room(bear_moved) 
    elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == True: 
     dead("The bear flies into a rage and chews your leg off.") 
    elif 'open' in next and bear_moved == True: 
     gold_room() 
    elif 'open' in next and bear_moved == False: 
     dead("The bear, still standing in the way, lunges up and rips your throat out.") 
    elif 'back' in next or 'backtrack' in next: 
     start() 
    else: 
     print "I got no idea what this means." 
     bear_room(bear_moved) 

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("> ").lower() 

    if 'flee' in next: 
     start() 
    elif 'head' in next: 
     dead("Well that was tasy!") 
    else: 
     cthuhlu_room() 

def dead(why): 
    print why, "Game Over!" 
    exit(0) 

def start(): 
    print "You're in a dark room." 
    print "You have no idea who you are or how you got here." 
    print "Your head is throbbing..." 
    print "There are two doors, one to your left, and one to your right." 
    print "Which door do you take?" 

    next = raw_input("> ").lower() 

    if 'left' in next: 
     bear_room(bear_moved) 
    elif 'right' in next: 
     cthulhu_room() 
    else: 
     start() 

start() 
+0

'bear_room'では、' bear_moved'を明示的に変数として渡します...そのため、グローバル変数ではなく、あなたが変更している 'bear_moved'という変数に** local **変数があります(あなたは' global bear_moved'をどこにも使用していませんが、その問題は最初に消えてしまいます)。 – donkopotamus

+1

** Minimal **、** Complete **、** Verifiable **コードの例を追加してみてください:http://stackoverflow.com/help/mcve –

+0

私はまだこの作業を行っています。単純なレベルでは、グローバル変数とローカル変数の違いを理解しています。グローバルブール変数、bear_movedを作成しました。これは、関数内から変数を変更できることに数えることができます。 –

答えて

2

はここで、あなたのbear_room機能のトップです。ルーチンへの呼び出しを適切に変更する必要があります。

ブール値についてはまだ学習していますが、あなたのテストで行った変更に注意してください。あなたはテストする必要はありませんbear_moved ==真;変数自体です。真/偽の値です。 との比較trueは何もしません。 との比較は、単にで、の操作ではありません。私はあなたのを削除しました。それ以外の場合は、に渡してください。bear_movedがNaN(数字ではありません)の場合、このプログラムには表示しないでください。論理的には、あなたはその句にまったく到達できませんでした。

もちろん、あなたのプログラムにはまだ改善があります:スペルや文法の誤りを修正し、ロジックの流れをもう少しきれいにしてください(あなたはこれについて何らかの流れをしましたか?仕事と読書時間を節約する。

def bear_room(): 
    global bear_moved 
    if not bear_moved: 
     print "There's a bear in 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?" 
    else: 
     print "The bear has moved away from the door." 
0

さらに、Pythonのグローバル変数とローカル変数についてさらに調査しました。私は今、関数内のローカル変数を変更していたことに気付きました。グローバルなbear_movedではなく変更されました。まさに、クマが再入国時に玄関の前で肥えた自己をプルーフした理由です。

bear_room()関数からパラメータを取り除き、bear_moved参照をグローバルにすると、関数は元のように機能しました。私はまた、bear_moved ==(真偽値)式を単にbear_movedまたはbear_movedに置き換えました。

from sys import exit 
bear_moved = False 

# Exercise 35 of Zed Shaw's LPTHW 
# Player can now revisit previous rooms 
# The bear_room function uses simple recursion 
# instead of the while loop of the original script 


def gold_room(): 
    print "This room is full of gold. How much do you take?" 
    next = raw_input("> ") 
    if next.isdigit(): 
     if int(next) > 101: 
      dead("You greedy bastard!") 
     elif int(next) < 101: 
      print "Nice, you're not greedy! You win!" 
      exit(0) 
     else: 
      pass 
    elif 'back' in next or 'backtrack' in next: 
     bear_room() 
    else: 
     print "Type a number!" 
     gold_room() 

def bear_room(): 

    """Globalizing bear_moved variable 
     and stripping parameter as advised.""" 

    global bear_moved 
    if not bear_moved: 
     print "There's a bear in 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?" 
    elif bear_moved: 
     print "The bear has moved away from the door." 


    next = raw_input("> ") 
    if 'honey' in next: 
     dead("The looks at you and then slaps your face off.") 
    elif 'taunt' in next and not bear_moved: 
     bear_moved = True 
     bear_room() 
    elif 'taunt' in next and bear_moved: 
     dead("The bear flies into a rage and chews your leg off.") 
    elif 'open' in next and bear_moved: 
     gold_room() 
    elif 'open' in next and not bear_moved: 
     dead("The bear, still standing in the way, lunges up and rips your throat out.") 
    elif 'back' in next or 'backtrack' in next: 
     start() 
    else: 
     print "I got no idea what this means." 
     bear_room() 

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("> ").lower() 

    if 'flee' in next or 'run' in next: 
     start() 
    elif 'head' in next: 
     dead("Well that was tasy!") 
    else: 
     cthulhu_room() 

def dead(why): 
    print why, "Game Over!" 
    exit(0) 

def start(): 
    print "You're in a dark room." 
    print "You have no idea who you are or how you got here." 
    print "Your head is throbbing..." 
    print "There are two doors, one to your left, and one to your right." 
    print "Which door do you take?" 

    next = raw_input("> ").lower() 

    if 'left' in next: 
     bear_room() 
    elif 'right' in next: 
     cthulhu_room() 
    else: 
     start() 

start() 
関連する問題