2012-05-04 4 views
-1
from sys import exit 
haskey = 0 

# start function 
def start(): 
print "You wake up in an empty room, feels like you've been here for days. You can't   remember anything from your past. All there is in the room is a digital clock. It says 3:26am, May 5, 2012. Get out of the room?" 

next = raw_input("> ").lower() 
if "yes" in next: 
    lobby() 
elif "no" in next: 
    print "We insist" 
else: 
    print "Try again." 

def lobby(): 
while True: 
    print "You arrived at a lobby, all you can see are four doors. Which door to enter? (first, second, third, fourth)?" 

    next = raw_input("> ").lower() 
    if "first" in next: 
     firstdoor() 
    elif "second" in next: 
     seconddoor() 
    elif "third" in next: 
     thirddoor() 
    elif "fourth" in next: 
     fourthdoor() 
    else: 
     print "Are you dumb and you can't even follow instructions?" 

def firstdoor(): 
print "You arrive at another empty room, examine further or exit?" 
choice = raw_input("> ").lower() 
if "examine" in choice: 
    print "A trap door opened, you fell in it and died." 
    exit() 
elif "exit" in choice: 
    lobby() 
else: 
    print "Are you dumb and you can't even follow instructions?" 

def seconddoor(): 
print "You arrive at the study room, examine room or exit?" 
choice = raw_input("> ").lower() 
if "examine" in choice: 
    print "There is a note on the table, read it?" 
    secondchoice = raw_input("> ").lower() 
    if "yes" in secondchoice: 
     note() 
    elif "no" in secondchoice: 
     print "Returning to lobby." 
     lobby() 

def note(): 
print """Security Log (040412): A man from the city travelling along the highway loses control of his vehicle and fell to the cliff. He was able to jump and grab a hold to the bushes growing at the side of the cliff. We were able to rescue him, but as soon as we secured him to the ground he violently reacted to our help and fainted. A few minutes later he was out of control, like he was possessed by a demon. We had no choice but to sedate him and keep him locked in our prison until authorities from the city arrive and examine him. The key to his cell is in the vault in the vault room. The keycode changes depending on the current date. 
""" 
print "Returning to lobby." 
lobby() 

def thirddoor(): 
if haskey == 0: 
    print "Door is locked, you need a key to continue." 
    print "%d" % haskey 
    lobby() 
elif haskey == 1: 
    exit() 

def exit(): 
print "You are now free!" 
print "To be continued.." 

def fourthdoor(): 
print "There is a vault inside the room. Use vault?" 
usevault = raw_input("> ") 
if "yes" in usevault: 
    vault() 
else: 
    print "Returning to lobby.." 
    lobby() 

def vault(): 
while True: 
    print "There is a security code for this door. Enter code:" 
    code = raw_input("> ") 
    if "05042012" in code: 
     print "Correct!" 
     print "Returning to lobby.." 
     haskey = int(1) 
     print "%d" % haskey 
     lobby() 
    else: 
     print "Code Error! Try again?" 

start() 

私はこのミニテキストゲームをPythonのチュートリアルに使用しています.4番ドア/ボールト機能を使用してプレーヤーにコードを尋ね、正しく入力された場合は使用する変数の値が変更されます3番目のドアを開くための鍵として。ボールトコードが正しく与えられたときに変数の値が変更されても、私はまだドアを開けません。変数値

誰でも手伝ってもらえますか?

+4

こんにちは、累計スタックオーバーフロー!質問を簡潔にするようにしてください。ほとんどの人はたくさんのコードを読んでいるわけではないので、問題を特定し、それを説明するために必要なコードだけを投稿すれば、より多くの回答を得ることができます。 – Shep

+3

"うわー!"とスタックは言った。 –

+0

また、誰も幅広いコードを読もうとはしません。ラインを短くしてください。 – geoffspear

答えて

0

Pythonの出会いvaultの内部haskey = int(1)が、それはあなただけvaultの内部を見ることができますhaskeyと呼ばれる新しいローカル変数を作成します。あなたは、その関数内でhaskeyと表示されたときに、ファイルの先頭に宣言するグローバルhaskeyを意味することをPythonに伝える必要があります。 vaultの先頭にglobal haskeyを追加すると、これを行うことができます。すなわち:

def vault(): 
    global haskey 
    while True: 
     print "There is a security code for this door. Enter code:" 
     code = raw_input("> ") 
     ... 
+1

また、 'int( 1) '、これまで。ちょうど '1'を使用 – Daenyth

+2

コードに間違ったことがたくさんあるので、私は自分の投稿の範囲を狭くすることに決めました。 :) –

+0

ようこそ! :)ありがとう:) –