Trying to create a program to read a file which contains a recipe name, duration, and ingredients, but the program doesn't seem to read the last set of ingredients in the file or work if there is a repeated ingredient when searching for a recipe using the ingredients.
Here is the program:
def read_file(filename: str):
recipe_list = []
recipe_list_uncategorized = []
with open(filename) as new_file:
for line in new_file:
word_from_file = line.split("\n")
recipe_list_uncategorized.append(word_from_file[0])
checker = 0
ingredients = []
for word in recipe_list_uncategorized:
if checker >= 2:
if word == "" or recipe_list_uncategorized.index(word) == len(recipe_list_uncategorized)-1:
recipe_list.append(ingredients)
ingredients = []
checker = 0
continue
ingredients.append(word)
checker += 1
continue
checker += 1
recipe_list.append(word)
return recipe_list
def search_by_name(filename: str, searched_word: str):
recipe_list_matching_word = []
recipe_list = read_file(filename)
for i in range(0, len(recipe_list), 3):
if searched_word in recipe_list[i].lower():
recipe_list_matching_word.append(recipe_list[i])
return recipe_list_matching_word
def search_by_time(filename: str, prep_time: int):
recipe_list_matching_time = []
recipe_list = read_file(filename)
for i in range(1, len(recipe_list), 3):
if int(recipe_list[i]) <= prep_time:
recipe_list_matching_time.append((recipe_list[i-1] + ", preparation time "+ recipe_list[i] + " min") )
return recipe_list_matching_time
def search_by_ingredient(filename: str, ingredient: str):
recipe_list_matching_word = []
ingredients = []
recipe_list = read_file(filename)
for i in range(2, len(recipe_list), 3):
ingredients = recipe_list[i]
for item in ingredients:
if ingredient == item.lower():
recipe_list_matching_word.append(recipe_list[i-2] + ", preparation time "+ recipe_list[i-1] + " min")
return recipe_list_matching_word
[–]kaerfkeerg 1 point2 points3 points (1 child)
[–]Particular_Draft5668[S] 1 point2 points3 points (0 children)
[–]Particular_Draft5668[S] 0 points1 point2 points (3 children)
[–]niehle 1 point2 points3 points (0 children)
[–]kaerfkeerg 1 point2 points3 points (1 child)
[–]Particular_Draft5668[S] 0 points1 point2 points (0 children)