在.py中创建python def

2024-06-16 10:04:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在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,并且我将能够为def和属性添加一个名称。你知道吗

我希望你能引导我找到一个方法来做这件事,如果这是不可能的,请说,我会重新考虑我的项目:)


Tags: 文件thepyimportnewifdefquestion
1条回答
网友
1楼 · 发布于 2024-06-16 10:04:21

你想在这里做的似乎是一个解决办法,至少在你试图处理它的方式。你知道吗

如果我正确地理解了这个问题,那么您正在尝试创建一个python脚本,该脚本接受用户的输入,如果该输入等于“new”,那么就让它能够定义一个新的动物名称。你知道吗

你目前正在使用大量的手工工作来处理这个问题,这将是非常难以扩展的,特别是考虑到你可能正在处理的数据集的大小(整个动物王国?)。你知道吗

你可以试着这样处理:

使用字典定义数据集:

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)

现在有了打印数据的方法。你知道吗

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

排除这些问题后,您可以继续添加输入: 在你的主.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

除此之外,值得一提的是,如果您使用dict和数组,您可以将内容放入数据库,并从中提取数据。你知道吗

希望这有帮助:)

相关问题 更多 >