2014-02-20 107 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

這將是很難乾淨利落地回答你的問題(S) - 所以我會特別關注超過配料問題的循環。這應該讓你指出正確的方向。

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)