I'm quite new to python, and as a little project, I am trying to make an interactive program where I can store recipes, each recipe will be stored as a list of the format: [Name, Servings, [List of ingredients], [List of steps in method]]
The first function, that creates the list works (i.e. I have created and stored in the file [Scrambled eggs, 1, [2 eggs, milk.....], [Beat the eggs....]]
However when I then call the 'view_recipes' function, I get:
Name: [
Servings: '
Ingredients:
S
Method:
c
so it is clearly iterating over characters in the string.
Is it a problem with how I write my list to my file? (I've looked this up before and everyone says you just need to have f.write(str(list))
Else it must be a problem with the reading of a file: but how can I get python to import it as a list of lists?
My code so far:
import re
#Input file
f = open("bank.txt", "r+")
def add_recipe():
recipe = []
ingredients = []
method = []
#recipe = [name, servings, ingredients(as list), method(as list)]
recipe.append(raw_input("Please enter a name for the dish: "))
recipe.append(raw_input("How many servings does this recipe make? "))
print "Ingredients"
ans = True
while ans:
i = raw_input("Enter amount and ingredient name i.e. '250g Flour' or 'x' to continue: ")
if i.lower() == "x":
ans = False
else:
ans = False
ingredients.append(i)
ans = True
print "Ingredients entered: "
for ing in ingredients:
print ing
recipe.append(ingredients)
print "Method: "
ans2 = True
while ans2:
j = raw_input("Type next step or 'x' to end: ")
if j.lower() == "x":
ans2 = False
else:
ans2 = False
method.append(j)
ans2 = True
print "Method: "
for step in method:
print step
recipe.append(method)
print "Writing recipe information to file..."
print recipe
f.write(str(recipe))
f.write("\n")
def view_recipes():
for line in f:
print "Name: ", list(line)[0]
print "Servings: ", list(line)[1]
print "Ingredients: "
for k in list(line)[2]:
print k
print "Method: "
for l in list(line)[3]:
print l