Search code examples
pythonvariables

making dynamically named variables in python


im new to python and im creating a program as sort of a practice exercise. It manages your plants around your house and tells you things like when to water them etc. Currently im working on the section that allows you to add a new plant. The problem im having is that im not sure how to make it so that each time i create a new plant, it has a new variable name. Here is my code:

class Plant:
    def __init__(self, name, location, wateringDays):
        self.name = name
        self.location = location
        self.wateringDays = wateringDays

def CreatePlant():
    name = input("Okay, what is this plant called?\n")
    location = input("Where in/around your house is this plant? (Multiple locations are fine. Located at:\n")
    wateringDay = input("What days do you water this plant? Please provide each day one at a time. Type 'done' when you are finished. Water this plant on:\n")
    wateringDays = []
    days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
    while wateringDay.lower() != "done":
        if wateringDay.lower() in days:
            wateringDays.append(wateringDay.lower())
            wateringDay = input("And:\n")
        else:
            wateringDay = input("Sorry, that is not a valid day of the week. Please try again.\n")
    else:
        print("\nGreat! You are all done making your plant.")

    Plant1 = Plant(name, location, wateringDays)

i set up a class to act as the basis for making plants. At the moment, it just puts all the values into a variable which is an object of that class. However, this obviously wont work as soon as i try to ad more than one plant, because it will just overwrite the existing one. How do i make it so that every time i add a new plant, it makes a new variable for it that doesnt already exist?

also sorry for any errors or inefficiencies in the code, i am very new to python.

i dont really know what to try, like i said im very new, i just know that i want to be able to add an infinite number of plants and have them assigned to a new variable each time.


Solution

  • class Plant:
      def __init__(self, id, name, location, wateringDays):
        self.id = id
        self.name = name
        self.location = location
        self.wateringDays = wateringDays
      
      def __repr__(self):
        return f"id = {self.id}, name = {self.name}, location = { self.location }, wateringDays = {self.wateringDays}"
    
    def printPlants(plantsList):
      print("\nPlant List")
      for plant in plantsList:
        print(plant)
    
    def getWateringDays():
      print("What days do you water this plant? Please provide each day one at a time. Type 'done' when you are finished")
      
      days = ("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday")
      wateringDays = []
      daysInputCompleted = False
      
      wateringDaysInputCompleted = False
      while not wateringDaysInputCompleted:
        wateringDay = input("Day or Done: ").lower()
        if wateringDay != "done":
          if wateringDay in days:
            wateringDays.append(wateringDay.lower())
          else:
            print("Sorry, that is not a valid day of the week. Please try again.")
        elif wateringDay == "done":
          wateringDaysInputCompleted = True
        else:
          print("Wrong input try again")
      
      return wateringDays
    
    def getPlantDetails():
      name = input("What is this plant called?\n")
      print()
      location = input("Where in/around your house is this plant? (Multiple locations are fine. Located at:\n")
      print()
      wateringDays = getWateringDays()
      print()
    
      return (name, location, wateringDays)
    
    
    def askIfPlantInputCompleted():
      while True:
        flag = input("Finish Creating plants? [y/n] ")
        if flag == 'y':
          return True
        elif flag == 'n':
          return False
        else:
          print(f"Wrong Input please enter either y/n, you entered: {flag}")
    
    def createPlants(plantsList):
      plantInputCompleted = False
      while not plantInputCompleted:
        name, location, wateringDays = getPlantDetails()
        plant = Plant(len(plantsList), name, location, wateringDays)
        plantsList.append(plant)
        plantInputCompleted = askIfPlantInputCompleted()
        print()
      print("\nGreat! You are all done making your plants.")
    
    plantsList = []
    createPlants(plantsList)
    printPlants(plantsList)
    

    You can try something like this

    What is this plant called?
    Rose
    
    Where in/around your house is this plant? (Multiple locations are fine. Located at:
    Roof
    
    What days do you water this plant? Please provide each day one at a time. Type 'done' when you are finished
    Day or Done: Monday
    Day or Done: Done 
    
    Finish Creating plants? [y/n] n
    What is this plant called?
    Lotus
    
    Where in/around your house is this plant? (Multiple locations are fine. Located at:
    Roof
    
    What days do you water this plant? Please provide each day one at a time. Type 'done' when you are finished
    Day or Done: done
    
    Finish Creating plants? [y/n] y
    
    Great! You are all done making your plants.
    
    Plant List
    id = 0, name = Rose, location = Roof, wateringDays = ['monday']
    id = 1, name = Lotus, location = Roof, wateringDays = []