0

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.

Ewan Weir
  • 3
  • 1
  • 3
    Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – Roshin Raphel Mar 05 '23 at 11:48
  • 1
    Rather than creating python variable names, you can try and store them as a key value pairs in a `Dictionary` or a `List` – Balaji Mar 05 '23 at 12:49
  • @Balaji by that do you mean assigning each object as a part of the dictionary? or each value of the object as a dictionary entry? sorry, im very new to this. Either way, wouldnt i still need to name objects and/or dictionaries? i just want the objects for each plant to have auto generated names so i dont have to set a limit to the number of plants. – Ewan Weir Mar 05 '23 at 12:59
  • The dict keys would be the generated names, and the values would be the actual objects that you would otherwise assign to variables. Dictionaries are specifically designed to relate "names" (more commonly called "keys") to "values", and to look up values by their keys. Plain variables are not intended for storing data with unknown/arbitrary names. – shadowtalker Mar 05 '23 at 14:04

1 Answers1

0
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 = []
Balaji
  • 795
  • 1
  • 2
  • 10