【发布时间】:2018-05-02 03:08:08
【问题描述】:
我的代码有问题。该循环仅运行一次,对应于用户输入的数字。感谢您的帮助。
#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的缩进保证它在第一次迭代后中断。也许您打算将其缩进到if条件内。 -
您的
break命令位于for循环内的主级别,因此它肯定会在第一次执行循环并结束时执行。尝试将break语句再缩进一级,将其放在前面的if语句中,看看是否能解决您的问题。
标签: python python-3.x