2016-07-22 78 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

相關問題