我正在嘗試創建一個RPG遊戲,並且讓代碼更加簡潔。我遇到了一個我無法解決的問題,而且我也對它進行了一些研究,但似乎無法解決這個問題。如何從導入文件中的函數導入列表?
我有我的主要代碼爲DragonFightingRPG.py
與我在同一目錄(prints.py
和options.py
)導入的其他文件。 我想從另一個文件中導入一個列表,因此它可以檢查用戶是否輸入了其中一個答案,並且可以相應地回答。
DragonFightingRPG.py
import prints
import options
class Player(object):
def __init__(self):
self.name = None
self.gold = None
self.maxhealth = 100
self.health = 100
def get_name(self):
self.name = input("Hey there! What's your name?\n~~> ")
print("Nice name, {0}!".format(self.name))
def give_gold(self):
print("Since you are new around here, 100 gold doubloons have been given to you!")
self.gold = 100
def gold_counter(self):
print("You currently have {0} gold!".format(player.gold))
player = Player()
def start():
player.get_name()
player.give_gold()
gold_counter()
prints.intro()
def gold_counter():
while True:
option = input("Do you want to see your balance?\n~~> ").upper()
if option in {options.yes_opt}:
print("You currently have {0} gold.".format(player.gold))
elif option in {options.no_opt}:
print("You can check your balance later in the game.")
else:
print("Please try again.")
continue
break
start()
prints.py
class Player(object):
def __init__(self):
self.name = None
self.gold = None
self.maxhealth = 100
self.health = 100
def get_name(self):
self.name = input("Hey there! What's your name?\n~~> ")
print("Nice name, {0}!".format(self.name))
def give_gold(self):
print("Since you are new around here, 100 gold doubloons have been given to you!")
self.gold = 100
def gold_counter(self):
print("You currently have {0} gold!".format(player.gold))
def intro():
print("Narrator: Uhhhm...")
print("N: Let me check my list...")
print("N: Ah! Yes! {0}, that's right. I heard you were supposed to be arriving today.".format(player.name))
print("N: Welcome to... THE DRAGON FIGHTER GAME!")
print("N: I know, it isn't the most imaginative name.")
print("N: Don't look at me like that, I tried my hardest!")
print("N: Anywho, let's carry on.")
print("N: For some stupid reason, the creator of this game didn't give me an actual name, so\nmy name is just \"Narrator\" or \"N\".")
print("N:")
player = Player()
options.py
def yes_opt():
{"Y", "YE", "YES", "YEAH", "PLEASE", "YES PLEASE"}
def no_opt():
{"N", "NO", "NOPE"}
的我正在努力的部分是elif option in {options.no_opt}:
和if option in {options.yes_opt}:
。它可以工作,如果我只是在主代碼中輸入列表,但我想嘗試從其他文件導入它。有什麼想法嗎?
當你想要的清單進口的,你爲什麼把它放在一個函數? 'yes_opt = {「Y」,「YE」,「是」,「YEAH」,「請」,「是請」}'有什麼問題? – dhke