2016-09-19 53 views
0

我想创建一个def文件内的py文件是外部例如。蟒蛇def创建一个.py

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以及属性。

我希望你能带领我的如何做到这一点的一种方式,如果它是不可能的,请只是说,我会重新考虑我的项目:)

+2

为什么你认为你需要为每个问题创建新的功能?每个功能都是一样的,只是数据不同;存储*只是数据*并具有加载和显示该数据的一个*函数。 –

+0

[Python定义动态函数]的可能重复(http://stackoverflow.com/questions/3687682/python-define-dynamic-functions) – iCart

回答

0

什么你想在这里做似乎有点解决方法,至少在你尝试处理它的方式上。

如果我正确地理解了这个问题,您正在尝试制作一个从用户获取输入的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) 

现在有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 

除此之外值得一提的是,如果你使用类型的字典和数组,你可以把东西放在数据库中,并从那里提取数据。

希望这有助于:)