python 3 import导入过多

时间:2019-06-26 21:15:13

标签: python python-3.x input import

多个python脚本的导入语句未按预期工作

我正在建立一个程序,以将游戏中的整个兽人存入python脚本。这里的问题是,它已经在行中打印了最后一个文件,这是不应该的。 “ bestiary.py”询问您要打开哪个类别,在其中,它应该询问您从该类别中具体想要哪个野兽。但是,即使在提供必要的输入之前,它已经可以打印出我拥有的整个野兽。

这是我的github存储库:https://github.com/Fuutralala/witcher-bestiary-py

我实际上不知道代码的哪一部分实际上被破坏了,我也无法弄清楚。我导入的方式似乎出了点问题,但是我找不到。

如果我给脚本提示输入,也会给我这个错误:

  File "bestiary.py", line 1, in <module>
    import draconids
  File "/home/fuutralala/Dropbox/PythonTrainingRepo/bestiary/draconids.py", line 13, in <module>
    Draconids()
  File "/home/fuutralala/Dropbox/PythonTrainingRepo/bestiary/draconids.py", line 10, in Draconids
    Cockatrice()
NameError: name 'Cockatrice' is not defined

2 个答案:

答案 0 :(得分:0)

尝试这个

# bestiary.py
from draconids import Draconids


def list():
    print("""
    - Beasts
    - Cursed Ones
    - Draconids
    - Elementa
    - Hybrids
    - Insectoids
    - Necrophages
    - Ogroids
    - Relicts
    - Specters
    - Vampires
    """)

def Bestiary():
    print("""
    Welcome to the Bestiary, where all the evil, malicious and
    devious Creatures are listed.
    Which category of monsters would you like to explore?
    """)
    list()


if __name__ == '__main__':
    Bestiary()
    val = input("> ")
    if val == "Draconids" or "draconids":
        Draconids()
    else:
        print("not ready yet")
# draconids.py
from cockatrice import Cockatrice


def Draconids():
    print("""
    About which Draconid would you like to learn more?
    - Basilisk
    - Cockatrice
    """)
    val = input("> ")
    if val == "Cockatrice" or "cockatrice":
        Cockatrice()
    else:
        print("not ready yet")

# cockatrice.py
def Cockatrice():
        print("""
        Cockatrice, also known as a skoffin and kurolishek, is an ornithosaur.
        It's also the only creature to belong to the order of ornithoreptile
        according to scholars, but why exactly they decided on this is not revealed.
        Its central tail feathers are also valuable as they're more durable and
        sharpen better than regular goose feathers for quills.
        """)


答案 1 :(得分:0)

您是正确的,问题出在导入。与其简单地调用Cockatrice(),还需要调用cockatrice.Cockatrice()。 或者,您可以执行from cockatrice import Cockatrice