我的代碼有問題。循環僅運行一次用戶輸入的號碼。謝謝您的幫助。循環只運行一次
#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
無論你的'for'循環中發生了什麼,'break'的縮進都會保證它在第一次迭代後中斷。也許你打算縮進它在'如果'條件。 – roganjosh
你在'for'循環的主要級別有'break'命令,所以在第一次執行循環並且循環結束時執行。嘗試將'break'語句縮進一級,放入前面的'if'語句中,看看是否解決了你的問題。 –