python def creation within a .py
<p>I am trying to create a def file within a py file that is external eg.</p>

<p><code>calls.py</code>:</p>

<pre><code>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())
</code></pre>

<p><code>mainwindow.py</code>:</p>

<pre><code>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 :")'''
</code></pre>

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

<p>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 :)</p>

<p>What you're trying to do here seems a bit of a workaround, at least in the way you're trying to handle it.</p>

<p>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.</p>

<p>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?).</p>

<p>You could try handling it like this:</p>

<p>define a data set using a dictionary:</p>

<pre><code>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.
</code></pre>

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

<pre><code>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())
</code></pre>

<p>You can then have a Whale class:</p>

<pre><code>class Whale(Animal)
</code></pre>

<p>Which now has the print_data method.</p>

<pre><code>for whale in whales:
    whales[whale].print_data()
</code></pre>

<p>With that out of the way, you can move on to adding input:
In your main.py:</p>

<pre><code>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
</code></pre>

<p>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.</p>

<p>Hope this helps :)</p>

python python-3.x