2014-02-20 4 views
-1

Я пытаюсь зациклить вход за выбранное количество времени «введите, как ингредиенты вы хотите:« скажите, если пользователь вводит 5, тогда «ингредиенты» цикла будут выполняться 5 раз для них для ввода их ингредиентов. Извините, если это похоже на основной вопрос, но я совершенно новичок в этом. Спасибо за любые ответы :).Запуск цикла (x) раз

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() 
+6

Просто голова, 'nextstuff',' nextstufff' и 'nextstuffff' не особенно легко различить ... – StephenTG

ответ

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) 
0

это не print ("ingredient") + (nextstufff), изменить его print ("ingredient:", nextstufff).

Или вы можете использовать строку форматирования:

print ("ingredient: %d"%nextstufff) 
Смежные вопросы