我正在尝试在外部的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
和属性添加一个名称
我希望你能引导我找到一种如何做到这一点的方法,如果不可能,请说出来,我会重新考虑我的项目:)
答案 0 :(得分: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
除此之外,值得一提的是,如果你使用dicts和数组,你可以把东西放在数据库中,然后从那里提取数据。
希望这会有所帮助:)