python def creation within a .py
Question: I am trying to create a def file within a py file that is external eg.

`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 :")'''
    

What I am trying to do is that `question == new` would create a new def in the
`calls.py` and that I would be able to add a name to the `def` and the
attributes as well.

I was hoping you could lead me to a way of how to do this, and if it is not
possible please just say and I will rethink my project :)


Answer: What you're trying to do here seems a bit of a workaround, at least in the way
you're trying to handle it.

If i understood the question correctly, you're trying to make a python script
that takes input from the user, then if that input is equal to "new", have it
be able to define a new animal name.

You're currently handling this using a whole lot of manual work, and this is
going to be extremely hard to expand, especially considering the size of the
data set you're presumably working with (the whole animal kingdom?).

You could try handling it like this:

define a data set using a dictionary:

    
    
    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.
    

This will build your data set. Presuming every `whale` class instance (if
there is one) inherits properties from a presumptive `Animal` class that
performs all the printing, say:

    
    
    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())
    

You can then have a Whale class:

    
    
    class Whale(Animal)
    

Which now has the print_data method.

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

With that out of the way, you can move on to adding input: In your 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
    

Beyond this it's worth mentioning that if you use dicts and arrays, you can
put things in a database, and pull your data from there.

Hope this helps :)

