简体   繁体   中英

How do I append multiple raw inputs to a list using a loop?

Title's pretty explanatory.

Here's where I'm at:

itemlist = list()
new_item = raw_input ("Input new item: ")

if new_item == "Done": break
else:
    itemlist.append(new_item)
    print itemlist
    continue

I'm fairly new at this so I'm having issues occasionally with placing the "continue" and "break" functions properly, so I keep getting errors with that

As for printing the itemlist, I never ends so I have to manually cancel it (CTRL + C)

I just want it so it'll loop back and ask me to continue inputting new items until I type "Done".

I know it's probably a simple solution.

The idiomatic thing to use in python is while True:

itemlist = list()
while True:
  new_item = raw_input ("Input new item: ")
  if new_item == 'Done':
    break
  itemlist.append(new_item)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM