我的代码出现问题。循环仅运行一次用户输入的数字。谢谢你的帮助。
#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
答案 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
答案 1 :(得分:0)
这样可行,因为你没有缩进" break"在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
答案 2 :(得分: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 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)
答案 3 :(得分: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
答案 4 :(得分: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)
答案 5 :(得分: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))