2017-11-18 178 views
1

我的代碼有問題。循環僅運行一次用戶輸入的號碼。謝謝您的幫助。循環只運行一次

#create an empty list and ask the user how many items they want to add 
# take the items the user entered and add it to the empty list. 
print("enter the number of items") 
x = int(input(">")) #take number of user input with the type int 
print("thanks") 
print("enter your food items") 
fitems = [] # empty list 
for i in range(x): #range to limit the number of iteration the user entered. 
    items = input (">") #taking the items from the user 
    empty = "" 
    if (items == empty):# checking if the user added nothing 
     print("no items added") 
    break 
else: 
    fitems.append(items) #add the list of items the user entered to the 
empty list 
    print("hello here is your food items: \n",fitems) #output user items 
+2

無論你的'for'循環中發生了什麼,'break'的縮進都會保證它在第一次迭代後中斷。也許你打算縮進它在'如果'條件。 – roganjosh

+0

你在'for'循環的主要級別有'break'命令,所以在第一次執行循環並且循環結束時執行。嘗試將'break'語句縮進一級,放入前面的'if'語句中,看看是否解決了你的問題。 –

回答

0

我冒昧地做出一些改變。試試看:

# create an empty list and ask the user how many items they want to add 
# take the items the user entered and add it to the empty list. 

fitems = [] 

# Define valid input for amount of items 
valid = [str(i) for i in range(1,10)] # ["1","2"...."9"] 

# If item is not in the list of valid, continue asking 
while True: 
    print("enter the number of items [1-9]") 
    x = input(">") 
    if x in valid: 
     break 

# Convert to int 
itemamount = int(x) 
print("thanks\nenter your food items") 

# Now ask for inputs using while > 0 and remove itemamount in each loop 
while itemamount > 0: 
    while True: 
     item = input (">") 
     if item: 
      break 
     print("Blank input") 
    fitems.append(item) 
    itemamount-=1 

print("hello here is your food items: ") 
for ind,i in enumerate(fitems,1): 
    print("{}. {}".format(ind,i)) 
0

這工作,因爲你沒有縮進「破發」裏面的for循環

#create an empty list and ask the user how many items they want to add 
# take the items the user entered and add it to the empty list. 
print("enter the number of items") 
x = int(input(">")) #take number of user input with the type int 
print("thanks") 
print("enter your food items") 
fitems = [] # empty list 
for i in range(x): #range to limit the number of iteration the user entered. 
    items = input (">") #taking the items from the user 
    empty = "" 
    if (items == empty):# checking if the user added nothing 
     print("no items added") 
     break #your error corrected 
    else: 
     fitems.append(items) #add the list of items the user entered to the 
    empty list 
    print("hello here is your food items: \n",fitems) #output user items 
+0

效果很好,但我希望打印語句在用戶輸入項目後打印所有內容,而不是打印一個項目,最後打印所有內容。 –

0

我想在你的代碼的唯一問題是identation,所以你應該這樣改變:

print("enter the number of items") 
x = int(input(">")) #take number of user input with the type int 
print("thanks") 
print("enter your food items") 
fitems = [] # empty list 
for i in range(x): #range to limit the number of iteration the user ente red. 
    items = input (">") #taking the items from the user 
    empty = "" 
    if (items == empty):# checking if the user added nothing 
     print("no items added") 
     break 
    else: 
     fitems.append(items) #add the list of items the user entered to the 

print("hello here is your food items: \n",fitems) 
+0

偉大的作品,但我想爲最後的打印聲明不打印後沒有任何輸入的用戶 –

1

您在代碼中缺少一些縮進,還有一些其他錯誤。這是(我認爲)正確的代碼:

print("enter the number of items") 
x = int(input(">")) 
print("thanks") 
print("enter your food items") 
fitems = [] 
for i in range(x): 
    items = input(">").strip() # there was a space before first bracket in your code, it's possible to have it there, but this is better 
    # edit: added .strip(), it removes extra whitespace from beginning and end 
    if items == "": # no need to create empty string variable, compare simply with empty string, 
    # edit: you can also use 'if not items:', because empty string evaluates to false 
     print("no items added") 
     break # break only if nothing added, so indent break to be only executed if empty 
    fitems.append(items) # you don't have to have else here, break ends the loop, so this is not executed then 
print("hello here are your food items: \n",fitems) # remove 1 indent, print only after loop ended 
+0

'if(items ==「」):'有不必要的括號,並且在大多數情況下,會更好地檢查有點虛僞。 「如果不是項目:'是更pythonic。 – roganjosh

+0

偉大的作品,但我希望打印聲明打印所有內容後,用戶輸入的項目,而不是打印一個,最後打印一切。 –

+0

@roganjosh括號在那裏,因爲我最近在寫一些JS。至於「不是項目」,我的父親(Python程序員)說我濫用這種行爲,我應該使用比較,因爲任何讀取代碼的人都是透明的。 –

0

首先break語句和else語句不是正確的。此外,當你使用.append()時,你應該添加一個i + = 1來循環x,用戶輸入。

這將工作:

for i in range(x): #range to limit the number of iteration the user entered. 
    items = input (">") #taking the items from the user 
    empty = "" 
    if (items == empty):# checking if the user added nothing 
     print("no items added") 
     break 
    else: 
     fitems.append(items) #add the list of items the user entered to the 
     i += 1 
0

這將導致無打印如果輸入什麼:

print("enter the number of items") 
x = int(input(">")) #take number of user input with the type int 
print("thanks") 
print("enter your food items") 
fitems = [] # empty list 
for i in range(x): #range to limit the number of iteration the user entered. 
    items = input (">") #taking the items from the user 
    empty = "" 
    if (items == empty):# checking if the user added nothing 
     print("no items added") 
     break 
    else: 
     fitems.append(items) #add the list of items the user entered to the\ 
                   empty list 
if len(fitems)==0: 
    pass 
else: 
    print("hello here is your food items: \n",fitems)