我需要从login()函数访问_startgame()函数。我用google搜索没有结果!
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")
答案 0 :(得分: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)
#...