我的代码有问题。循环仅运行一次用户输入的号码。谢谢您的帮助。循环只运行一次
#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'语句中,看看是否解决了你的问题。 –