运行循环(x)次

时间:2014-02-20 16:01:56

标签: python loops python-3.x

我试图在一段选定的时间内输入一个输入“输入你想要的成分:”如果用户输入5,那么一个循环“成分”将运行5次,以便他们输入他们的成分。对不起,如果它看起来像一个基本问题,但我对此很新。谢谢你的回答:)。

a=input("hello, welcome to the recipe calculator \nenter your recipe name, number of      people you will serve and the list of ingredients \n1.retrive recipe \n2.make recipe \noption:")

if a=="2":
    print("now enter the name of your recipe")
    f=open('c:\\username_and_password.txt', 'w')
    stuff = input("create name:")
    nextstuff = (int(input("enter number of people:")))
    nextstufff = (int(input("enter how many ingredients you want:"))) 
    for (nextstufff) in range(0, 10): #I have put 0, 10 because the user probably wont put more than 10 ingredients. But there's probably a much better way?
        print ("ingredient") + (nextstufff) #what am I doing wrong in this for loop

    nextstuffff = (int(input("so how much of\n" + nextstufff + "would you like:")))
    f.write(str(stuff + "\n") + str(nextstuff) + str(nextstufff))
    f.close()

2 个答案:

答案 0 :(得分:1)

很难干净地回答你的问题 - 因此我将专注于循环成分问题。这应该让你指向正确的方向。

def read_items(prompt):
    items = []
    while True:
        answer = raw_input('%s (blank to end): ' % prompt)
        if not answer:
            return items
        items.append(answer)

ingredients = read_items('enter ingredients')
counts = []
for item in ingredients:
    cnt = raw_input('how much of %s:' % item)
    counts.append(int(cnt))

print zip(ingredients, counts)

答案 1 :(得分:0)

不是print ("ingredient") + (nextstufff),请将其更改为print ("ingredient:", nextstufff)

或者您可以使用字符串格式:

print ("ingredient: %d"%nextstufff)