2017-06-19 104 views
0

我正在嘗試創建一個RPG遊戲,並且讓代碼更加簡潔。我遇到了一個我無法解決的問題,而且我也對它進行了一些研究,但似乎無法解決這個問題。如何從導入文件中的函數導入列表?

我有我的主要代碼爲DragonFightingRPG.py與我在同一目錄(prints.pyoptions.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}:。它可以工作,如果我只是在主代碼中輸入列表,但我想嘗試從其他文件導入它。有什麼想法嗎?

+2

當你想要的清單進口的,你爲什麼把它放在一個函數? 'yes_opt = {「Y」,「YE」,「是」,「YEAH」,「請」,「是請」}'有什麼問題? – dhke

回答

1
def yes_opt(): 
    {"Y", "YE", "YES", "YEAH", "PLEASE", "YES PLEASE"} 

定義一個函數稱爲yes_opt(其中有效什麼都不做),而不是一個與該名稱設置

因此,option in yes_opt不起作用。

你可能意味着簡單地寫這個:

yes_opt = {"Y", "YE", "YES", "YEAH", "PLEASE", "YES PLEASE"} 
1

通過一些小的改變,你可以得到這個工作。

  1. options.py

您應該從方法返回這些集合。

def yes_opt(): 
    return {"Y", "YE", "YES", "YEAH", "PLEASE", "YES PLEASE"} 

def no_opt(): 
    return {"N", "NO", "NOPE"} 
  • DragonFightingRPG.py,可以指定作爲from options import yes_opt, no_opt導入語句。
  • 當你調用這些函數時,它們會返回相應的集合。

    if option in yes_opt(): 
    

    依此類推。