运行循环(x)次

2024-09-30 04:41:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试循环输入一个选定的时间量“输入你想要的成分的方式:”例如如果用户输入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()

Tags: ofthenameyounumberinputyourrecipe
2条回答

很难清晰地回答你的问题,所以我将重点放在配料循环问题上。这会让你找到正确的方向。在

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)

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

也可以使用字符串格式:

print ("ingredient: %d"%nextstufff)

相关问题 更多 >

    热门问题