2016-07-22 70 views
0

现在我正在尝试为我的朋友做一个小游戏Python 3.3。 这一切都很顺利,直到我忘记了如何通过不同的功能(Menu,Hills)来获得某些变量(在我的情况下为黄金,破坏和XP)。如何在Python 3.3中正确声明全局变量,以便它们可以在多个函数中工作?

如果有人能帮我修复这段代码,那么上述三个变量可以在我的函数之间保持不变,那将是惊人的。这是我的代码。

import os 
import random 
import time 

def Menu(): 
    global damage 
    global xp 
    global gold 
    damage = 1 
    xp = 0 
    gold = 0 
    print("\nyour attack value is", damage,"\nyour xp level is", xp,"\nand you have", gold,"gold.\n") 
    sword = input("Old Joe Smith: Do you need a sword? ") 
    sword = sword.lower() 
    if sword == "yes": 
     damage = damage + 9 
     print("Old Joe Smith: *Gives you a sharp steel sword* Good luck on your travels!\n\nYour new attack value is",damage,"\n") 
     time.sleep(2) 
     Hills() 
    if sword == "yeah": 
     damage = damage + 9 
     print("Old Joe Smith: *Gives you a sharp steel sword* Good luck on your travels!\n\nYour new attack value is",damage,"\n") 
     time.sleep(2) 
     Hills() 
    elif sword == "no": 
     print("Old Joe Smith: Well, if you say so... ...good luck anyway!\n") 
     Hills() 
    else: 
     print("Old Joe Smith: I'm sorry, what?") 
     time.sleep(1) 
     Menu() 

def Hills(): 
    print("*You walk through the forest, when out of nowhere a minotaur appears!*") 
    fight = input("What will you do, run or fight? ") 
    fight = fight.lower() 
    if fight == "run": 
     print("You escape, barely.") 
     Cave() 
    if fight == "fight": 
     input("Press enter") 
     if damage > 5: 
      print("You win! you looted 10 gold and got 5 xp.") 
      gold = gold + 10 
      xp = xp + 5 
      Cave() 
     elif damage < 5: 
      print("You died. Game over.") 
      Menu() 
     else: 
      print("How the hell did you get exactly 5 damage?") 
      Menu() 
    else: 
     print("Your lack of a proper response makes the minotaur charge. It kills you instantly.") 
     Menu() 

def Cave(): 
    print("You stumble into a cave. there are two routes in the cave. which way do you want to go, left or right?") 

print("Welcome to 'RPG Game', a role-playing game developed in Python 3.3.\nThis game was developed by bamf_mccree.\n") 
print("Old Joe Smith: Hello, adventurer, and welcome to Dankhill, in the centre of Whitewood forest. \nThis was once a peaceful place, but the evil Lord Draktha has enslaved most of the civilians of our realm.") 
time.sleep(4) 
Menu() 
+1

我想这已经回答了几次。 试试这个答案: http://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python – Ramalus

+0

我不知道如何将其转移到我的职能。 –

回答

1

在这种情况下,我想你只需要声明的本地方法范围之外的那些变量,当你修改它们使用global关键字。这样的事情:

import os 
import random 
import time 

damage = None 
xp = None 
gold = None 

def Menu(): 
    damage = 1 
    print("\nyour attack value is", damage,"\nyour xp level is", xp,"\nand you have", gold,"gold.\n") 
    sword = input("Old Joe Smith: Do you need a sword? ") 
    sword = sword.lower() 
    if sword == "yes": 
     damage = damage + 9 
     print("Old Joe Smith: *Gives you a sharp steel sword* Good luck on your travels!\n\nYour new attack value is",damage,"\n") 
     time.sleep(2) 
     Hills() 
    if sword == "yeah": 
     damage = damage + 9 
     print("Old Joe Smith: *Gives you a sharp steel sword* Good luck on your travels!\n\nYour new attack value is",damage,"\n") 
     time.sleep(2) 
     Hills() 
    elif sword == "no": 
     print("Old Joe Smith: Well, if you say so... ...good luck anyway!\n") 
     Hills() 
    else: 
     print("Old Joe Smith: I'm sorry, what?") 
     time.sleep(1) 
     Menu() 

def Hills(): 
    print("*You walk through the forest, when out of nowhere a minotaur appears!*") 
    fight = input("What will you do, run or fight? ") 
    fight = fight.lower() 
    if fight == "run": 
     print("You escape, barely.") 
     Cave() 
    if fight == "fight": 
     input("Press enter") 
     if damage > 5: 
      print("You win! you looted 10 gold and got 5 xp.") 
      global gold 
      gold = gold + 10 
      global xp 
      xp = xp + 5 
      Cave() 
     elif damage < 5: 
      print("You died. Game over.") 
      Menu() 
     else: 
      print("How the hell did you get exactly 5 damage?") 
      Menu() 
    else: 
     print("Your lack of a proper response makes the minotaur charge. It kills you instantly.") 
     Menu() 

def Cave(): 
    print("You stumble into a cave. there are two routes in the cave. which way do you want to go, left or right?") 

print("Welcome to 'RPG Game', a role-playing game developed in Python 3.3.\nThis game was developed by bamf_mccree.\n") 
print("Old Joe Smith: Hello, adventurer, and welcome to Dankhill, in the centre of Whitewood forest. \nThis was once a peaceful place, but the evil Lord Draktha has enslaved most of the civilians of our realm.") 
time.sleep(4) 
Menu() 
+0

如果这对你有帮助,请选择我的答案作为选择的答案。很高兴我能帮上忙。 – Ramalus

相关问题