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()
'bear_room'では、' bear_moved'を明示的に変数として渡します...そのため、グローバル変数ではなく、あなたが変更している 'bear_moved'という変数に** local **変数があります(あなたは' global bear_moved'をどこにも使用していませんが、その問題は最初に消えてしまいます)。 – donkopotamus
** Minimal **、** Complete **、** Verifiable **コードの例を追加してみてください:http://stackoverflow.com/help/mcve –
私はまだこの作業を行っています。単純なレベルでは、グローバル変数とローカル変数の違いを理解しています。グローバルブール変数、bear_movedを作成しました。これは、関数内から変数を変更できることに数えることができます。 –