Apr-18-2020, 09:58 AM
In this code, I just try to get data from user about animal nutrition, respiratory, excretory and reproductive. Then, I put these animal data class by user animal type input. After that i create a file to save those data. It is just a exercise to improve myself. My question is that even i put nutrition or etc data function in animal main class. I had to code another function for nutrition or etc. for input specification. Is there any other simple way to do that without righting extra input function. For example, At the beginning of the code i had tried to put those input function into animal main class or subclasses of it. I couln't manage that.
Also, I would like to hear your improvement advise for my code.
Thank You for Your Time.
Also, I would like to hear your improvement advise for my code.
Thank You for Your Time.
# all class class animal: def __init__(self, nutrition = "gg", respiratory = "gg", excretory = "gg", reproductive = "gg"): self.nutrition = nutrition self.respiratory = respiratory self.excretory = excretory self.reproductive = reproductive class land(animal): def __init__(self, nutrition, respiratory, excretory, reproductive, climate, animal_type): super().__init__(nutrition, respiratory, excretory, reproductive) self.climate = climate self.animal_type = animal_type def land_show_info(self): return "Animal Type: {}\nNutrition: {}\nRespiratory: {}\nExcretory: {}\nReproductive: {}\nClimate: {}\n".format( self.animal_type, self.nutrition, self.respiratory, self.excretory, self.reproductive, self.climate) class sea(animal): def __init__(self, nutrition, respiratory, excretory, reproductive, climate, animal_type): super().__init__(nutrition, respiratory, excretory, reproductive) self.climate = climate self.animal_type = animal_type def land_show_info(self): return "Animal Type: {}\nNutrition: {}\nRespiratory: {}\nExcretory: {}\nReproductive: {}\nClimate: {}\n".format( self.animal_type, self.nutrition, self.respiratory, self.excretory, self.reproductive, self.climate) class air(animal): def __init__(self, nutrition, respiratory, excretory, reproductive, climate, animal_type): super().__init__(nutrition, respiratory, excretory, reproductive) self.climate = climate self.animal_type = animal_type def land_show_info(self): return "Animal Type: {}\nNutrition: {}\nRespiratory: {}\nExcretory: {}\nReproductive: {}\nClimate: {}\n".format( self.animal_type, self.nutrition, self.respiratory, self.excretory, self.reproductive, self.climate) # all class input function def nutrition(): while True: nutrition = input(""" Please Enter Nutrition Type 1. Carnivorous --> 'c' 2. Herbivorous --> 'h' 3. Omnivorous --> 'o' 4. No Information --> 'n'\n""") if nutrition == 'c': nutrition = "Carnivorous" break elif nutrition == 'h': nutrition = "Herbivorous" break elif nutrition == 'o': nutrition = "Omnivorous" break elif nutrition == 'n': nutrition = "No Information" break else: print("""!WARNING! ...Improper Input Detected...""") return nutrition def respiratory(): while True: respiratory = input(""" Please Enter Respiratory Type 1. with Oxygen --> '+o2' 2. without Oxygen --> '-o2' 3. No Information --> 'n'\n""") if respiratory == '+o2': respiratory = "with Oxygen" break elif respiratory == '-o2': respiratory = "without Oxygen" break elif respiratory == 'n': respiratory = "No Information" break else: print("""!WARNING! ...Improper Input Detected...""") return respiratory def excretory(): while True: excretory = input(""" Please Enter Excretory Type 1. Ammonia --> 'a' 2. Urea --> 'u' 3. Uric Acid --> 'ua' 4. No Information --> 'n'\n""") if excretory == 'a': excretory = "Ammonia" break elif excretory == 'u': excretory = "Urea" break elif excretory == 'ua': excretory = "Uric Acid" break elif excretory == 'n': excretory = "No Information" break else: print("""!WARNING! ...Improper Input Detected...""") return excretory def reproductive(): while True: reproductive = input(""" Please Enter Reproductive Type 1. Sexual --> 's' 2. Asexual --> 'a' 3. No Information --> 'n'\n""") if reproductive == 's': reproductive = "Sexual" break elif reproductive == 'a': reproductive = "Asexual" break elif reproductive == 'n': reproductive = "No Information" break else: print("""!WARNING! ...Improper Input Detected...""") return excretory def climate(): while True: climate = input(""" Please Enter Climate Type 1. Desert --> 'd' 2. Forest --> 'f' 3. Tundra --> 't' 4. Ice Field --> 'i' 5. No Information --> 'n'\n""") if climate == 'd': climate = "Desert" break elif climate == 'f': climate = "Forest" break elif climate == 't': climate = "Tundra" break elif climate == 'i': climate = "No Ice Field" break elif climate == 'n': climate = "No Information" break else: print("""!WARNING! ...Improper Input Detected...""") return climate def animal_type(): while True: animal_type = input(""" Please Enter Animal Type 1. Land --> 'l' 2. Sea --> 's' 3. Air --> 'a'\n""") if animal_type == 'l': animal_type = "Land" break elif animal_type == 's': animal_type = "Sea" break elif animal_type == 'a': animal_type = "Air" break else: print("""!WARNING! ...Improper Input Detected...""") return animal_type # input from user nutrition = nutrition() respiratory = respiratory() excretory = excretory() reproductive = reproductive() climate = climate() animal_type = animal_type() # animal classification if animal_type == 'Land': animal1 = land(nutrition, respiratory, excretory, reproductive, climate, animal_type) print(animal1.land_show_info()) elif animal_type == 'Sea': animal1 = sea(nutrition, respiratory, excretory, reproductive, climate, animal_type) print(animal1.land_show_info()) else: animal1 = air(nutrition, respiratory, excretory, reproductive, climate, animal_type) print(animal1.land_show_info()) # Is there a better way to check file is there or not by program itself while True: file_ = input("""Is there a file on C:/Users/Gökberk/Desktop/Animal List.txt directory\n(y/n)""") if file_ == "y": with open("C:/Users/Gökberk/Desktop/Animal List.txt", "a", encoding="utf-8") as file: file.write("##############################\n") file.write(animal1.land_show_info()) break elif file_ == "n": with open("C:/Users/Gökberk/Desktop/Animal List.txt", "w", encoding="utf-8" ) as file: file.write("...Welcome to Animal List File...\n") file.write("##############################\n") file.write(animal1.land_show_info()) print("File has been created to C:/Users/Gökberk/Desktop/Animal List.txt") break else: print("""!WARNING! ...Improper Input Detected...""")