1
我做Python中的食譜,並用一類這樣做與屬性刪除元素
class.name
- 配方名
class.ing_count
- Python字典 - 成分
class.ings
數含有成分名稱,數量和金額單位。
我想讓它刪除配方中的特定成分,但這不起作用。有什麼想法嗎?
刪除成分
searchrecipe = raw_input("Recipe to edit:)
if edit_ing == 1:
del_ing = raw_input("Ingredient to Delete: ")
del_ing.title()
#Search Ingredient list
for recipe in TheBook:
if recipe.name == searchrecipe:
del recipe.ings[del_ing]
break
所以你的問題是......?怎麼了?爲什麼它不工作?你能給我們一個你班上的[mcve]嗎? –
您在第一行中缺少引號。 – Quill
此外,與所有'str'方法一樣,'del_ing.title()'返回一個新的字符串,它不會改變現有的字符串。您需要重新指定'del_ing'來使更改保持不變。 – ShadowRanger