![]() |
Class Improvement - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code Review (https://python-forum.io/forum-46.html) +--- Thread: Class Improvement (/thread-26000.html) |
Class Improvement - Gokberk - Apr-18-2020 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. # 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...""") RE: Class Improvement - michael1789 - Apr-18-2020 My first thought is to have a single class (Animal) and a single input function. If all the different types of information and inputs can be stored in dictionaries thus: nutrition = {1 : "Carnivorous", 2 : "Herbivorous", 3 : "Omnivorous", 4 : "No Information", input_text : """Please Enter Nutrition Type 1. Carnivorous 2. Herbivorous 3. Omnivorous 4. No Information"""}Then if you put all those dictionaries in to another one (eg, Animal.info_dict) then you can have all these inputs as part of the __init__ for the single animal class. RE: Class Improvement - Gokberk - Apr-19-2020 Sounds correct. However, I'm newbie. Can you show me how to do that in an simple example RE: Class Improvement - michael1789 - Apr-19-2020 You'll have to take a bit of time to learn dictionaries. They are super useful and worth your time. https://www.youtube.com/watch?v=daefaLgNkw0&t=384s is great video. This is not a complete working example. But after you watch that video or read up on dictionaries a little I can help you further. Any way you want to do it is just as good, but this is an idea to cycle through a dictionary of dictionaries and produce a final dictionary that holds all the info for that particular animal. It's just a rough suggestion. As I said, familiarize yourself with dictionaries and I'll be happy to work you through it as best I can. Class Animal: def __init__(self): self.dictionaries = {1 : nutrition, #<-- dicts like my previous post 2 : respiratory, 3 : excretory, 4 : reproductive, 5 : climate} self.data = {} self.input_function() def input_function(self): for i in range(1, 5): answer = input(self.dictionaries[i][input_text] self.data.update(i : self.data[i][answer]) RE: Class Improvement - Gokberk - Apr-19-2020 Thank you for your positive feed back. I'm glad to here that. I will do my best (Apr-19-2020, 08:13 PM)michael1789 Wrote: You'll have to take a bit of time to learn dictionaries. They are super useful and worth your time. https://www.youtube.com/watch?v=daefaLgNkw0&t=384s is great video. |