2016-09-19 6 views
0

私は外部のpyファイル内にdefファイルを作成しようとしています。.py内でのPythonの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 == newcalls.pyに新しいDEFを作成することを、私はに名前を追加することができるだろうということですdefとその属性も含まれています。

私はあなたがこれを行う方法の道に私を導くことができ期待して、それが不可能な場合だけで言うと、私は私のプロジェクトを再考しますしてくださいました:)

+2

質問ごとに新しい機能を作成する必要があると思われるのはなぜですか?各関数は同じデータを扱います。 *データだけを保存し、そのデータをロードして表示する* 1つの*関数を持っています。 –

+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. 

これはあなたのデータセットを構築します。 (存在する場合)、すべての印刷を行う推定Animalクラスからプロパティを継承するすべてのwhaleクラスのインスタンスを推定、言う:

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 

を、それはあなたがdictsと配列を使用している場合ことを言及する価値がある、あなた物事をデータベースに入れてそこからデータを引き出すことができます。

希望します。

関連する問題