Error when converting working code to a more object oriented structure Error: string indices must be integers Python 3
Error when converting working code to a more object oriented structure Error: string indices must be integers Python 3
我将 运行 放入错误中:在我的脚本中使用占位符时,字符串索引必须是整数。
我的程序应该纯粹根据计算来跟踪蔬菜的生长。这个想法是每个植物都有自己的特征(例如胡萝卜数据),但我没有为每个 'plantData' 编写代码,而是将代码替换为(whichPlant 和后来的 whichPlantData)作为临时占位符(这样我就不需要新的我花园里的每一种植物的代码,或者我想在以后添加的每一种植物的代码。
这是我在最后一行(植物是 class)中用 *** 标记的错误。当我使用 (carrotData) 而不是 (whichPlantData) 时,我的脚本有效。但是一旦我放入临时占位符 (whichPlantData) 就中断了。
这是什么原因造成的(这样我就可以避免在以后的项目中这样做),我该如何解决这个问题?
感谢支持!!
carrotData = {'plantID': '','plantingTime': dt(year=now.year, month=3, day=1), "dateOfPlanting": 0, "numberOfPlants": 0, "germinationTime": 7, "growthTime": 227, "flowerTime": 247, "harvestTime": 254, "liveCycles": 1, "status": 0}
potatoData = {'plantID': '','plantingTime': dt(year=now.year, month=3, day=1), "dateOfPlanting": 0, "numberOfPlants": 0, "germinationTime": 7, "growthTime": 227, "flowerTime": 247, "harvestTime": 254, "liveCycles": 1, "status": 0}
print ("imported plant datasheets")
#functions:
#if plant is added
def addPlant():
whichPlant = input("Which plant do you want to add? ")
n = int(input("How many plants do you want to add? "))
i = 0
whichPlantData = whichPlant + "Data"
if whichPlant in inventory:
while i < n:
i += 1
if whichPlant in plants:
plants[whichPlant] += 1
else:
plants[whichPlant] = 1
***Error*** whichPlant = Plant("", whichPlantData['plantingTime'], dt.now(), n, dt.now() + timedelta(days=whichPlantData['germinationTime']), dt.now() + timedelta(days=whichPlantData['growthTime']), dt.now() + timedelta(days=whichPlantData['flowerTime']),whichPlantData['harvestTime'], whichPlantData['liveCycles'], whichPlantData['status'])
您的问题似乎与 whichPlantData = whichPlant + "Data"
有关。 whichPlant 是由 input
函数返回的字符串。我认为您要做的是根据用户的输入获取植物信息字典。此外; whichPlant + "Data"
似乎试图使 whichPlant
与指向植物信息字典的变量名相同。仅仅因为字符串 whichPlant
可能等于变量名 carrotData
并不能使它与变量相同。我建议制作一个包含植物信息的字典列表,然后遍历该列表中的项目以查看字典 name
键是否与用户输入相同。
类似这样:
plants = [{"Name": "Carrot", 'plantID': '','plantingTime':0, "dateOfPlanting": 0, "numberOfPlants": 0, "germinationTime": 7, "growthTime": 227, "flowerTime": 247, "harvestTime": 254, "liveCycles": 1, "status": 0},
{"Name": "Potato", 'plantID': '','plantingTime': 0, "dateOfPlanting": 0, "numberOfPlants": 0, "germinationTime": 7, "growthTime": 227, "flowerTime": 247, "harvestTime": 254, "liveCycles": 1, "status": 0}]
PlantName = input("Enter a Plant: ")
for plant in plants:
if plant['Name'] == PlantName:
print("{}'s germinationTime is {}".format(PlantName, plant["germinationTime"]))
# DO SOMETHING
我将 运行 放入错误中:在我的脚本中使用占位符时,字符串索引必须是整数。
我的程序应该纯粹根据计算来跟踪蔬菜的生长。这个想法是每个植物都有自己的特征(例如胡萝卜数据),但我没有为每个 'plantData' 编写代码,而是将代码替换为(whichPlant 和后来的 whichPlantData)作为临时占位符(这样我就不需要新的我花园里的每一种植物的代码,或者我想在以后添加的每一种植物的代码。
这是我在最后一行(植物是 class)中用 *** 标记的错误。当我使用 (carrotData) 而不是 (whichPlantData) 时,我的脚本有效。但是一旦我放入临时占位符 (whichPlantData) 就中断了。
这是什么原因造成的(这样我就可以避免在以后的项目中这样做),我该如何解决这个问题?
感谢支持!!
carrotData = {'plantID': '','plantingTime': dt(year=now.year, month=3, day=1), "dateOfPlanting": 0, "numberOfPlants": 0, "germinationTime": 7, "growthTime": 227, "flowerTime": 247, "harvestTime": 254, "liveCycles": 1, "status": 0}
potatoData = {'plantID': '','plantingTime': dt(year=now.year, month=3, day=1), "dateOfPlanting": 0, "numberOfPlants": 0, "germinationTime": 7, "growthTime": 227, "flowerTime": 247, "harvestTime": 254, "liveCycles": 1, "status": 0}
print ("imported plant datasheets")
#functions:
#if plant is added
def addPlant():
whichPlant = input("Which plant do you want to add? ")
n = int(input("How many plants do you want to add? "))
i = 0
whichPlantData = whichPlant + "Data"
if whichPlant in inventory:
while i < n:
i += 1
if whichPlant in plants:
plants[whichPlant] += 1
else:
plants[whichPlant] = 1
***Error*** whichPlant = Plant("", whichPlantData['plantingTime'], dt.now(), n, dt.now() + timedelta(days=whichPlantData['germinationTime']), dt.now() + timedelta(days=whichPlantData['growthTime']), dt.now() + timedelta(days=whichPlantData['flowerTime']),whichPlantData['harvestTime'], whichPlantData['liveCycles'], whichPlantData['status'])
您的问题似乎与 whichPlantData = whichPlant + "Data"
有关。 whichPlant 是由 input
函数返回的字符串。我认为您要做的是根据用户的输入获取植物信息字典。此外; whichPlant + "Data"
似乎试图使 whichPlant
与指向植物信息字典的变量名相同。仅仅因为字符串 whichPlant
可能等于变量名 carrotData
并不能使它与变量相同。我建议制作一个包含植物信息的字典列表,然后遍历该列表中的项目以查看字典 name
键是否与用户输入相同。
类似这样:
plants = [{"Name": "Carrot", 'plantID': '','plantingTime':0, "dateOfPlanting": 0, "numberOfPlants": 0, "germinationTime": 7, "growthTime": 227, "flowerTime": 247, "harvestTime": 254, "liveCycles": 1, "status": 0},
{"Name": "Potato", 'plantID': '','plantingTime': 0, "dateOfPlanting": 0, "numberOfPlants": 0, "germinationTime": 7, "growthTime": 227, "flowerTime": 247, "harvestTime": 254, "liveCycles": 1, "status": 0}]
PlantName = input("Enter a Plant: ")
for plant in plants:
if plant['Name'] == PlantName:
print("{}'s germinationTime is {}".format(PlantName, plant["germinationTime"]))
# DO SOMETHING