2015-04-06 73 views
2

我试图实现我迄今为止学到的一些东西到一个简单的文本游戏,但我遇到了麻烦,试图在一个函数中更改全局变量,以便它保持改变另一个。无法更改函数的全局变量

keynumb = 0 

这是全局变量。我试图将它更改为1内功能,然后调用它在另一个(if 1 open the door, if 0 don't)等..

已经尝试了大约一个小时,直到我的大脑刚刚死在我身上,希望有人能指出我的'做错了。

我在代码中添加了星号。

干杯。

from sys import exit 

keynumb = 0 


def start(): 
    print "You are in a room with two doors in front of you. One on the left, and one on the right." 
    while True: 
     door_choice = raw_input("Do you take the left or right door? Your choice: ") 
     if "left" in door_choice or "Left" in door_choice: 
      dark_room() 
     elif "right" in door_choice: 
      monster_room() 
     else: 
      print "I don't understand." 

def dark_room(): 
    print "You find yourself in a dark room, unable to see anything." 
    while True: 
     dr_choice = raw_input("What do you do?") 
     if "light" and "switch" in dr_choice or "turn" and "light" in dr_choice: 
      print "You've turn on the light and the room is illuminated." 
      print "On the floor is a small silver key." 
      dr_choice_light = raw_input("What do you do?") 
      if "pick" in dr_choice_light: 
       print "You pick up the key and exit back the way you came." 
       keynumber() ************* 
       start() 
      elif "nothing" in dr_choice_light: 
       print "You do nothing. Nothing happens." 
      elif "back" in dr_choice_light or "exit" in dr_choice_light: 
       start()  
     else: 
      print "That didn't do anything."   


def monster_room(): 
    print "It seems this door is locked and requires a key." 
    while True: 
     mons = raw_input("What do you do?") 
     if "key" in mons and keynumb == 0: ************** 
      print "You don't have a key, dummy. Might as well turn back.." 
     elif "key" in mons and keynumb == 1: 
      print "You open it using the small silver key." 
      print "You enter the room and a huge monster looks up from his iPhone. \"You want to get past me?\" he says. Well, if you know what 4 + 4 is then I'll let you pass." 
      answer = raw_input("What is 4+4?") 
      if answer == "8": 
       print "\"Well done!\" says the monster. He smiles as you pass to the door behind him." 
      else: 
       print "Stupid. You die." 
       exit 
     elif "back" in mons: 
      print "You turn back." 
      start()  
     else: 
      print "I don't understand." 

def keynumber(): ********** 
    global keynumb += 1 
    return keynumb 


start() 
+0

测验:这条线有什么问题:dr_choice中的if“light”和“switch”或dr_choice中的“turn”和“light”:'? –

+0

'dr_choice'应该在光线之后来回转动,否则我只是在问'如果'点亮''? (其中,大猜想在这里..就像一个布尔?如果它只是一个if语句,则返回True/False?) – iliketoeatcandles

+0

@isherwood或者,添加到^(编辑太晚)..我的另一个猜测是,是整个做法,甚至是修改后的方式,因为太多和/或不完整而没有意义?有没有办法,也许使用逗号分隔两个字符串,所以它可能是这样的:如果Dr_choice中的“light”,“switch”或Dr_choice中的“turn”,“light”....? – iliketoeatcandles

回答

3

你的语法是错误的keynumber

你想要的东西像

def keynumber(): 
    global keynumb 
    keynumb += 1 

我离开了return语句,因为你似乎并不在那里使用也无妨

+0

啊辉煌,欢呼声,完美的作品。认为我是在复杂它的方式。 – iliketoeatcandles

+0

'exit'是一个函数。当他死亡时,把它称作'exit()'在你的怪物房间功能 –

+0

啊,欢呼! – iliketoeatcandles