python-3.x 导入错误:无法从"种族"导入名称"character_name"

6pp0gazn  于 2023-01-03  发布在  Python
关注(0)|答案(1)|浏览(172)

我正在构建一个基于文本的游戏,在那里你输入命令,它们使用if/elif/else语句执行。
我目前的目标是使一个文件在字符创建过程中,该程序将能够从拉,但我陷入了这个导入错误,并在几个小时的谷歌搜索后有点损失。

Traceback (most recent call last):
  File "c:\Users\AlexZ\OneDrive\Documents\Heros-of-Norubia\Core-Game.py", line 1, in <module>
    from Main_Menu import main_menu
  File "c:\Users\AlexZ\OneDrive\Documents\Heros-of-Norubia\Main_Menu.py", line 3, in <module>
    from Character_Creation import character_creation
  File "c:\Users\AlexZ\OneDrive\Documents\Heros-of-Norubia\Character_Creation.py", line 1, in <module>
    from Race import character_name 
ImportError: cannot import name 'character_name' from 'Race' (c:\Users\AlexZ\OneDrive\Documents\Heros-of-Norubia\Race.py)

Core-Game.py 从主菜单导入主菜单main_menu()
主菜单.py

from Race_Selection import race_info
from Race_Selection import race_choice
from Character_Creation import character_creation
def main_menu():
    print("Hello!! And welcome to Norubia. Please Choose your race.")

    race_info()
    race_choice()
    character_creation()

人种选择.py

from elf import elfinfo
from dwarf import dwarfinfo
from giant import giantinfo
from goblin import goblininfo
from human import humaninfo
from minotaur import minotaurinfo
from orc import orcinfo
from troll import trollinfo
from vampier import vampireinfo
from millennial_vampire import millennialvampireinfo
#from dwarf import dwarfselect
#from Character_Creation import character_file
from Race import dwarf
def race_info():
    print("You can choose from the following Races below.")
    print("To learn more about the available races type the race name with a '?' after")
    print("\nPress 'q' at any time to quit")

def race_choice():
    print("\nPlease Choose From The Races Below\nDwarf\nElf\nGiant\nGoblin\nHuman\nMinotaur\nOrc\nTroll\nVampire\nMillennial Vampire")
    player_race = input("What is your race? > ")
    if player_race == 'Dwarf?':
        dwarfinfo()
        race_choice()
    if player_race == 'Elf?':
        elfinfo()
        race_choice()
    if player_race == 'Giant?':
        giantinfo()
        race_choice()
    if player_race == 'Goblin?':
        goblininfo()
        race_choice()
    if player_race == 'Human?':
        humaninfo()
        race_choice()
    if player_race == 'Minotaur?':
        minotaurinfo()
        race_choice()
    if player_race == 'Orc?':
        orcinfo()
        race_choice()
    if player_race == 'Troll?':
        trollinfo()
        race_choice()
    if player_race == 'Vampire?':
        vampireinfo()
        race_choice()
    if player_race == 'Millennial Vampire?':
        millennialvampireinfo()
        race_choice()
    if player_race == 'q':
        exit()
    if player_race == 'Dwarf':
        dwarf()
    else:
        print("That is not a valid selection")
        race_choice()

Race.py

def dwarf():
    player_race = 'Dwarf'
    plyer_name = input("What is your name Dwarf? > ")

人物_创作.py

from Race import character_name 
def character_creation():
    def character_file():
        character_name = input(f"What is your name {player_race.title()}")
        f = open(f"{character_name}.txt", "x")
        f.write(f"Player Name {character_name.title()}")
        f.close()

我希望character_name能从Race文件正确导入

o3imoua4

o3imoua41#

您的Race.py文件只定义了dwarf函数,而不是character_name,因此当您尝试从该文件导入character_name时,将得到ImportError
此外,除了在Character_Creation.pycharacter_creation函数内部之外,您似乎没有在任何地方定义character_name,您已经导入了character_name函数,因此使用此范围的新定义函数隐藏了您的导入。

相关问题