【问题标题】:python def creation within a .py在 .py 中创建 python def
【发布时间】:2016-09-19 09:52:38
【问题描述】:

我正在尝试在外部的 py 文件中创建一个 def 文件,例如。

calls.py:

def printbluewhale():
    whale = animalia.whale("Chordata",
                  "",
                  "Mammalia",
                  "Certariodactyla",
                  "Balaenopteridae",
                  "Balaenoptera",
                  "B. musculus",
                  "Balaenoptera musculus",
                  "Blue whale")

    print("Phylum - " + whale.getPhylum())
    print("Clade - " + whale.getClade())
    print("Class - " + whale.getClas())
    print("Order - " + whale.getOrder())
    print("Family - " + whale.getFamily())
    print("Genus - " + whale.getGenus())
    print("Species - " + whale.getSpecies())
    print("Latin Name - "+ whale.getLatinName())
    print("Name - " + whale.getName())

mainwindow.py:

import calls
import animalist
#import defs

keepgoing = 1

print("Entering main window")
while True:
    question = input("Which animal would you like to know about?" #The question it self
                     + animalist.lst) #Animal Listing


    if question == "1":
        print(calls.printlion())#Calls the animal definition and prints the characteristics 

    if question == "2":
        print(calls.printdog())

    if question == "3":
        print(calls.printbluewhale())

    '''if question == "new":
        def new_animal():
            question_2=input("Enter the name of the new animal :")'''

我想要做的是question == new 将在calls.py 中创建一个新的定义,并且我将能够为def 添加一个名称以及属性。

我希望你能引导我找到如何做到这一点的方法,如果不可能,请直接说,我会重新考虑我的项目 :)

【问题讨论】:

  • 您认为为什么需要为每个问题创建新函数?每个函数都做同样的事情,只是使用不同的数据; 只存储数据,并拥有一个函数来加载和显示该数据。

标签: python python-3.x


【解决方案1】:

您在这里尝试做的似乎是一种解决方法,至少在您尝试处理它的方式上。

如果我正确理解了这个问题,您正在尝试制作一个接受用户输入的 python 脚本,然后如果该输入等于“新”,那么它是否能够定义一个新的动物名称。

您目前正在使用大量手动工作来处理此问题,而且这将非常难以扩展,尤其是考虑到您可能正在使用的数据集的规模(整个动物王国?)。

你可以尝试这样处理:

使用字典定义数据集:

birds = dict()
fish = dict()

whales = dict()
whales["Blue Whale"] = animalia.whale("Chordata",
                  "",
                  "Mammalia",
                  "Certariodactyla",
                  "Balaenopteridae",
                  "Balaenoptera",
                  "B. musculus",
                  "Balaenoptera musculus",
                  "Blue whale")

whales["Killer Whale"] = ... # just as an example, keep doing this to define more whale species.

animals = {"birds": birds, "fish": fish, "whales": whales} # using a dict for this makes you independent from indices, which is much less messy.

这将构建您的数据集。假设每个 whale 类实例(如果有的话)都从执行所有打印的假定 Animal 类继承属性,例如:

Class Animal():

    # do some init

    def print_data(self):
        print("Phylum - " + self.getPhylum())
        print("Clade - " + self.getClade())
        print("Class - " + self.getClas())
        print("Order - " + self.getOrder())
        print("Family - " + self.getFamily())
        print("Genus - " + self.getGenus())
        print("Species - " + self.getSpecies())
        print("Latin Name - "+ self.getLatinName())
        print("Name - " + self.getName())

然后你可以有一个鲸类:

class Whale(Animal)

现在有 print_data 方法。

for whale in whales:
    whales[whale].print_data()

有了这些,您可以继续添加输入: 在你的 main.py 中:

while True:
    question = input("Which animal would you like to know about?" #The question it self
                     + animalist.lst) #Animal Listing

    try:
        id = int(question)
        # if the input can be converted to an integer, we assume the user has entered an index.
        print(calls.animals[animals.keys[id]])
    except:
        if str(question).lower() == "new": # makes this case insensitive
            new_species = input("Please input a new species")
            calls.animals[str(new_species)] = new_pecies
            # here you should process the input to determine what new species you want

除此之外,值得一提的是,如果您使用字典和数组,您可以将内容放入数据库,然后从那里提取数据。

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    相关资源
    最近更新 更多