Python 循环只运行一次

Python 循环只运行一次,python,python-3.x,Python,Python 3.x,我的代码有问题。循环仅运行一次用户输入的相应编号。谢谢你的帮助 #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 in

我的代码有问题。循环仅运行一次用户输入的相应编号。谢谢你的帮助

#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”

#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

我认为您的代码中唯一的问题是标识,因此您应该这样更改:

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)

代码中缺少一些缩进,还有一些其他错误。这是(我认为)正确的代码:

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

首先,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

如果未输入任何内容,这将导致不打印:

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)

我冒昧地做了一些修改。试试这个:

# 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))

无论在
for
循环中发生了什么,
break
的缩进保证它在第一次迭代后中断。也许您想将其缩进到
if
条件中。您的
break
命令位于
for
循环的主级别,因此它肯定会在第一次执行循环和循环结束时执行。尝试将
break
语句再缩进一级,将其放在前面的
if
语句中,看看这是否解决了您的问题。
if(items==“”):
有不必要的括号,在大多数情况下,最好只检查是否有错误<代码>如果不是项目:更像Python。效果很好,但我希望print语句在用户输入项目后打印所有内容,而不是打印一个项目,最后打印所有内容。@roganjosh括号在那里,因为我最近写了一些JS。至于
非项
,我爸爸(python程序员)说我滥用这种行为,我应该使用比较,因为它对任何阅读代码的人都是透明的。@JonathanAkweteyOkine我不确定我是否理解你。。。现在,代码将在循环结束后打印所有项。如果您想让它在用户每次添加项目时打印所有项目,请在最后一个
print
语句之前添加一个缩进级别(4个空格),这样它就会进入循环并在每次迭代中执行。@JonathanAkweteyOkine我看您是堆栈溢出新手,所以这里有一个提示-如果是这样(当然也可以是其他情况)答案解决了你的问题,你发现它是最好的,标记为接受(勾选)。See效果很好,但我希望print语句在用户输入项目后打印所有内容,而不是打印一条,最后打印所有内容。效果很好,但我希望最后一条print语句在用户未输入任何内容后不打印