2015-05-19 72 views
-1

我需要從login()函數訪問_startgame()函數。我搜索了這個沒有結果!直接調用嵌套函數

import random 
import player 
#import hashlib 
#import :P 
import random 
import time 

gamename = "foobarbaz" 

class Player(object): 
    def __init__(self, name, maxhp, minhp, maxmana, minmana, maxskill, minskill): 
     self.hp = random.randint(minhp, maxhp) 
     self.mana = random.randint(minmana, maxmana) 
     self.skill = random.randint(minskill, maxskill) 
     self.name = name 
     #self.race = race 
     self.gold = 200 + random.randint(50, 200) 
     self.inventory = [] 


    def check_dead(self): 
     if self.hp < 0: 
     print ("You Died...") 
     self.inventory = [] 
     self.gold = 0 
     return 0 
     else: 
     return self.hp 

def prompt(x): 
    print(x.name + ":" + str(x.hp) + " HP") 




def new_player(): 
    name = input("First, who are you?\n>>") 
    print ("Now, please select a class:") 
    print ("\t[R]anger") 
    print ("\t[W]arrior") 
    print ("\t[M]age") 
    print ("\t[O]Rouge") 
    print ("\t[P]aladin") 
    print ("\t[B]arbarian") 
    print ("\t[C]leric") 
    print ("\t[D]ruid") 
    print ("\t[H]ealer") 
    print ("\t[K]Dark mage") 
    theclass = input("Please type the letter of the class:\n>>") 
    if theclass.upper() == "R": 
     theplayer = Player(name, 200, 150, 100, 50, 100, 50) #ranger attributes 
     aclass = "Ranger" 
    elif theclass.upper() == "W": 
     theplayer = Player(name, 250, 190, 70, 20, 125, 75) 
     aclass = "Warrior" 
    elif theclass.upper() == "M": 
     theplayer = Player(name, 225, 200, 350, 200, 100, 75) 
     aclass = "Mage" 
    elif theclass.upper() == "O": 
     theplayer = Player(name, 325, 300, 10, 0, 150, 100) 
     aclass = "Rouge" 
    elif theclass.upper() == "P": 
     theplayer = Player(name, 225, 100, 50, 100, 150, 100) 
     aclass = "Paladin" 
    elif theclass.upper() == "B": 
     theplayer = Player(name, 400, 250, 1, 0, 100, 50) 
     aclass = "Barbarian" 
    elif theclass.upper() == "C": 
     theplayer = Player(name, 200, 190, 100, 50, 200, 190) 
     aclass = "Cleric" 
    elif theclass.upper() == "D": 
     theplayer = Player(name, 125, 100, 450, 400, 125, 100) 
     aclass = "Druid" 
    elif theclass.upper() == "H": 
     theplayer = Player(name, 325, 300, 250, 200, 150, 100) 
     aclass = "Healer" 
    elif theclass.upper() == "K": 
     theplayer = Player(name, 125, 100, 500, 450, 150, 100) 
     aclass = "Dark Mage" 
    else: 
     print("Please select a letter of a class") 
     new_player() 
     return 

    def _startgame(): 
     print("Welcome to " + gamename + " " + name) 
     print("To Exit, type exit\nYou can type anywhere you see") 
     prompt(theplayer) 


     return 

    print("You have selected a " + aclass + " called "+ name + ".") 
    sure = input("Are you sure?[Y/N]") 
    if sure.upper() == "N" or sure.upper() == "NO": 
    new_player() 
    return 
    print("No going back now!") 
    print("Your charicter has " + str(theplayer.hp) + " health, " + str(theplayer.mana) + " mana, and " + str(theplayer.skill) + " skill.") 

    file = open("user.txt", "w") 
    file.write(name + "\n") 
    file.close() 
    userfile = open("users/" + name + ".txt", "w") 
    userfile.close() 
    _startgame() 



def login(): 
    username = input("Username: ") 
    if username in open('user.txt').read(): 
     print ("Existing Profile Loaded") 







    else: 
     print("You are not known to the world of " + gamename) 
     print("Creating new account...") 
     time.sleep(1) 
     new_player() 
    return 

startgame = input("If you are a new player type new \nif you are an existing player type login\n>>") 



if startgame.lower() == "new": 
    new_player() 

if startgame.lower() == "login": 
    login() 

def prompt(x): 
    print(x.name + ":" + str(x.hp) + " HP") 
+1

你不能做到這一點。只需將'_startgame'移出'new_player' - 這裏沒有任何理由。 –

回答

1

在Python中,當執行流程離開這樣的範圍時,在某個範圍內創建的所有對象(當然包括函數)都是「垃圾回收」。所以,該函數(_startgame)僅在執行new_player時才存在。

像你綁定一個函數或變量的一類,除非你寫這樣的代碼不能在函數或變量綁定到另一個功能:

def bar(): 
    pass 

def foo(): 
    pass 

foo.bar = bar 
foo.bar() 

解決您的問題:

正如AdamSmith在他的評論中指出的那樣,創建一個函數(或者移動那個已經擁有的函數)可以消除create_player函數。

# You need your new function takes three parameters. 
def _startgame(gamename, name, theplayer): 
     print("Welcome to " + gamename + " " + name) 
     print("To Exit, type exit\nYou can type anywhere you see") 
     prompt(theplayer) 

然後你就可以,例如,把它在功能new_player(或任何你想要的)是這樣的:

#... 
_startgame(gamename, name, theplayer) 
#... 
+0

我在new_player中擁有它的原因是因爲那是我似乎只能從 – wizardsambolton

+0

訪問名稱變量的唯一地方。但是對於我發佈的解決方案,您可以將該變量作爲參數傳遞給該函數。 –

+0

好吧我試過,並得到錯誤:NameError:名稱'名稱'未定義 – wizardsambolton