I'm trying to make a python program that I can insert a recipe, and I want to make a list of the ingredients. I know how to insert data as a certain spot in the list, using.insert, but when I use a for loop, I keep getting an error that says "TypeError: insert expected 2 arguments, got 1." I don't know what I'm doing wrong. Please help.
print("")
ingred=[]
measure=[]
n=0
i=0
for n in range(0,n+1):
ingred[n]=ingred.insert(input("What is the ingredient?(type done to end ingrediants)"))
n=n+1
measure[i]=measure.insert(input("How much of the ingredient?"))
i=i+1
Use the append function:
print("")
ingred=[]
measure=[]
n=10
for n in range(0,n+1):
ingred.append(input("What is the ingredient?(type done to end ingrediants)"))
measure.append(input("How much of the ingredient?"))
I don't really understand your n=n+1
code, so I removed it here - this should just run N times, which I set to be 10.
The insert()
function takes two arguments, the element to insert and the index to insert it. To insert a recipe at the end of a list, use append()
.
You don't add to a list by assigning to an index. You use the append()
method.
And if you want to stop when they enter end
, you need to assign the input to a variable so you can check it before appending to the list.
while True:
name = input("What is the ingredient?(type done to end ingrediants)")
if name == 'done':
break
ingred.append(name)
measure.append(input("How much of the ingredient?"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.