2017-11-18 234 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)