2016-04-03 79 views
0

林新(-ish)到Python编辑在函数内部变量,我今天我完成后,我意识到我犯了一个大错误做了一个游戏:如何访问和蟒蛇

我有函数中访问和编辑变量,这些变量在其他函数中也可以访问和更改,也可能在未来的函数之外。我不知道该怎么做。

我研究了很长时间,发现很少可能解决问题的东西,我尝试了几个,但他们没有工作,我不明白如何使用他人。

能不能请你帮我这个问题,如果你发现别人请你告诉我,因为我不是太擅长调试:(

这里是下面的代码,其相当大的(我已经把变量我需要访问和大胆的改变):从随机进口randint 打印( “鬼游戏V2.0”) 打印( “选择了难度”)

score  = 0 
alive  = True 
difficulty = 0 
doors  = 0 
ghost_door = 0 
action  = 0 
ghost_power = 0 
       #define the function 'ask_difficulty' 
def ask_difficulty() : 
    difficulty = input ("Hard, Normal, Easy") 
    set_difficulty() 



       # define the function 'set_difficulty' which sets the   difficulty. 
def set_difficulty() : 
if difficulty == 'Hard' or 'Normal' or 'Easy' : 
    if difficulty == 'Hard' : 
     doors = 2 

    elif difficulty == 'Normal' : 
     doors = 3 

    elif difficulty == 'Easy' : 
     doors = 5 

else: 
    print ("Invalid input, please type Hard, Normal, or Easy") 
    ask_difficulty() 



      # define the function 'ghost_door_choose' which sets the ghost door and the chosen door 


def ghost_door_choose(x): 
    ghost_door = randint (1, x) 

    print (doors + " doors ahead...") 
    print ("A ghost behind one.") 
    print ("Which do you open?") 

    if doors == 2 : 
     door = int("Door number 1, or door number 2...") 
     if 1 or 2 in door : 
      ghost_or_no() 

     else : 
      print ("Invalid input") 
      ghost_door_choose(difficulty) 


    elif doors == 3 : 
     door = int("Door number 1, door number 2, or door number 3") 
     if 1 or 2 or 3 in door : 
      ghost_or_no() 

     else: 
      print ("Invalid input") 
      ghost_door_choose(difficulty) 


    elif doors == 5 : 
     print("Door number 1, door number 2, door number 3, door number 4,  or door number 5.") 
     if 1 or 2 or 3 or 4 or 5 in door : 
      ghost_or_no() 

     else: 
      print ("Invalid input") 
      ghost_door_choose(difficulty) 


       # define the function 'ghost_or_no' 
def ghost_or_no() : 
    if door == ghost_door: 
     print ("GHOST!!") 
     print ("Initiating battle...") 
     battle() 

    else: 
     print ("No ghost, you\'ve been lucky, but will luck remain with you...") 
     score = score + 1 
     ghost_door_choose(difficulty) 

       # define the function 'battle' which is the battle program 
def battle() : 
    ghost_power = randint (1, 4)    # 1 = Speed, 2 = Strength, 3 = The ghost is not friendly, 4 = The ghost is friendly 

    print ("You have 3 options") 
    print ("You can flee, but beware, the ghost may be fast (flee),") 
    print ("You can battle it, but beware, the ghost might be strong (fight),") 
    print ("Or you can aproach the ghost and be friendly, but beware, the ghost may not be friendly (aproach)...") 
    action = input ("What do you choose?") 

    if flee in action : 
     action = 1 

    elif fight in action : 
     action = 2 

    elif aproach in action : 
     action = 3 

    else : 
     print ("Invalid input") 
     battle() 

    if ghost_power == action : 
     if action == 1: 
      print ("Oh no, the ghost\'s power was speed!") 
      print ("DEFEAT") 
      print ("You\'r score is " + score) 
      alive = False 

     elif action == 2: 
      print ("Oh no, the ghost\'s power was strength!") 
      print ("DEFEAT") 
      print ("You\'r score is " + score) 
      alive = False 

     elif action == 3: 
      print ("Oh no, the ghost wasn\'t friendly ") 
      alive = False 

    elif ghost_power == 4 and action == 3 : 
     print ("Congratulations, The ghost was friendly!") 
     score = score + 1 
     ghost_door_choose(difficulty) 

    elif ghost_power != action and ghost_power != 4 : 
     if action == 1: 
      print ("Congratulations, the ghost wasn\'t fast!") 
      score = score + 1 
      ghost_door_choose(difficulty) 

     elif action == 2: 
      print ("Congratulations, you defeated the ghost!") 
      score = score +1 
      ghost_door_choose(difficulty) 

    elif ghost_power != action and ghost_power == 4 : 
     if action == 1: 
      print ("You ran away from a friendly ghost!") 
      print ("Because you ran away for no reason, your score is now 0") 
      score = 0 
      ghost_door_choose(difficulty) 
     elif action == 1: 
      print ("You killed a friendly ghost!") 
      print ("Your score is now 0 because you killed the friendly ghost") 
      score = 0 
      ghost_door_choose(difficulty) 



        #actual game loop 

ask_difficulty() 

while alive : 
    ghost_door_choose(doors) 
+0

有点太多的代码... – SteJ

+2

您显示的代码太多。如果你缩小到[mcve],它会帮助读者。 – idjaw

+1

如果您想要共享和更新各种功能中的变量,您可能需要一个类 –

回答

0

考虑:

x=0 
z=22 
def func(x,y): 
    y=22 
    z+=1 
    print x,y,z 

func('x','y')  

当你调用func你会得到UnboundLocalError: local variable 'z' referenced before assignment

要修正这个错误在我们的功能,这样做:

x=0 
z=22 
def func(x,y): 
    global z 
    y=22 
    z+=1 
    print x,y,z 

global关键字允许的本地引用全局定义的变量改变。

请注意,本地版本x已打印,而不是全局版本。这是你所期望的。不明确的地方在于没有本地版本的值。除非您使用global关键字,否则Python会将全局定义的值视为只读。

正如评论所述,一个保留这些变量的类会更好。

+1

这帮了我很多!谢谢,我可能会这样做,如果你能向我解释什么是类,我会很感激,我对Python很新,而且还没有碰到它。感谢您花时间解释这一点。 – Pixelf

0

脚本顶部的那些变量是全局变量,要在函数中设置它们,必须在函数中声明它们是全局变量。作为一个较小的例子,

score = 0 
alive = True 

def add_score(value): 
    """Add value to score""" 
    global score 
    score += value 

def kill_kill_kill(): 
    global alive 
    alive = False 

下一步是创建类,这可能会变得复杂。例如,如果你想跟踪用户的分数,但用户可以有多个角色,每个角色都有自己的活力,你就可以开始建立类来表示这些东西。

0

全局关键字可能是你在找什么。

例如在下面的代码中。

some_variable = 10 

def some_function(): 
    global some_variable 
    some_variable = 20 

这将导致some_variable(在全球范围内)指的是20值在哪里,因为这将保持在10(在全球范围内),而不使用全球关键字。

有关全局和局部变量的更多信息here

+0

我已经解决了不太好但略简单的全局关键字所使用的问题。我很感激帮助,不再需要任何更多建议或答案。 – Pixelf

0

一个函数有自己的变量范围 - 对于很多语言来说都是如此。这意味着一旦函数完成执行,变量就不存在了(Python的垃圾收集会清除它们)。

这样做的做法是使用全局变量(Old Variables)。老派(并且通常会皱眉,不一定公平)。这些变量是你在函数范围之外声明的变量,通常在源代码的开头,并且可以在整个程序的各种函数和类中使用。

有很好的理由,人们不会使用全局变量,从性能问题到混淆本地作用域变量,但它们是一种快速简便的方法,可以在整个程序中保存信息并访问它。

要使用一个全球性的,你需要的是您正在使用的变量的函数内声明,就像这样:

MyGlobal="This is a global variable" 
def MyFunction(): 
    global MyGlobal 
    MyGlobal += " and my function has changed it" 
if __name__=="__main__": 
    print MyGlobal 
    MyFunction() 
    print MyGlobal 

话虽如此,传递信息并从功能,通常的方法是使用参数和返回值 - 这是一个更好的设计方法,通常教授的方法。这是一个设计方法,而不是对代码的更改;你编写程序的时候要把全局变量降到绝对最小值。

为了拿上面的例子,这将我们的代码更改为以下:

def MyFunction(MyArg): 
    MyArg+=" and my function has given me a new version of it" 
    return MyArg 
if __name__=="__main__": 
    MyVariable="This is a variable" 
    print MyVariable 
    MyVariable = MyFunction(MyVariable) 
    print MyVariable 

注意,这是更加灵活 - 我可以使用它,因为我有以上,改变MyVariable的值,但我也可以使用相同的函数将新值返回到其他变量,从而保持原始值不变。

我希望这可以帮助,对不起,如果我有点冗长。