在 python 中创建动态命名的变量

问题描述 投票:0回答:0

我是 python 的新手,我正在创建一个程序作为练习。它管理您房子周围的植物,并告诉您何时给它们浇水等。目前我正在研究允许您添加新植物的部分。我遇到的问题是我不确定如何制作它,以便每次我创建一个新工厂时,它都有一个新的变量名称。这是我的代码:

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)

我建立一个类作为制作植物的基础。目前,它只是将所有值放入一个变量中,该变量是该类的一个对象。然而,这显然不会在我尝试为不止一种植物添加广告时起作用,因为它只会覆盖现有的植物。我如何做到每次添加新植物时,它都会为它创建一个尚不存在的新变量?

对于代码中的任何错误或效率低下,我也很抱歉,我是 python 的新手。

我真的不知道该尝试什么,就像我说的我很新,我只知道我希望能够添加无限数量的植物并每次都将它们分配给一个新变量。

python variables
© www.soinside.com 2019 - 2024. All rights reserved.