-2

I would like the user to be able to add items to a list. For example, if I was asking the user to create a shopping list of a certain length(user determines length), how do I do that? So far all I've been able to come up with is how to ask them the length of their list, but now I'm stuck

lengthOfList = int(input("How many items are in your list? "))

shoppingList = []
for i in range(lengthOfList):
    shoppingList.append(i)
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
oneman
  • 811
  • 1
  • 13
  • 31
  • No need to yell in all-caps, there are homework questions here all the time. Just be polite and say it is homework. On a side note, is this Python 2 or 3? – Alyssa Haroldsen Mar 29 '16 at 03:42
  • Wasn't yelling, I was just putting it in caps so that people who didn't want to help with homework questions knew straight away – oneman Mar 29 '16 at 03:42
  • Currently you are appending the list index position. Ask for input again and append the inputted item. – Dan Mar 29 '16 at 03:43
  • I assume you can use `input` in your for loop as well – Xiongbing Jin Mar 29 '16 at 03:43
  • 1
    Is there a requirement that you need to know your list length ahead of time? If not, that is unneeded. You can append to a list until the user indicates they have completed adding items – Andy Mar 29 '16 at 03:43
  • Yes so the main thing I'm struggling with is the input part, because there can be different numbers of input depending on how many items are in the list – oneman Mar 29 '16 at 03:44
  • The question just asks that the user be able to enter as many items as they would like, hence the `lengthOfList` part – oneman Mar 29 '16 at 03:45
  • @LiamEmery Unless specifically required by the homework problem, I'd recommend not asking the user at the beginning how many items are on the list, but wait until the user inputs some sort of terminator (usually nothing). – Alyssa Haroldsen Mar 29 '16 at 03:46
  • @Kupiakos would i change `for i in range(lengthOfList)` to `for i in range()` ? Or is that incorrect syntax – oneman Mar 29 '16 at 03:51
  • @LiamEmery You wouldn't use `range` at all as the size of the list would not be known beforehand. Lists are dynamically sized in Python. Also, kudos on using backticks correctly. – Alyssa Haroldsen Mar 29 '16 at 03:54
  • @Kupiakos Hmmm okay, would I be able to have a hint please on what I would change? Not the full answer just a point in the right direction, thank you – oneman Mar 29 '16 at 03:56
  • 1
    @LiamEmery The basic idea is to have a `while` loop which will run until the user inputs an empty value. It will continually ask the user for their next item, and if it is not empty, add it to the list. Otherwise, we're done asking and we can exit our loop. – Alyssa Haroldsen Mar 29 '16 at 03:59

1 Answers1

-1

How about something like this?

shoppingList = []
newItem = input('What is your first item?')
while newItem != '':
    shoppingList.append(newItem)
    newItem = input('What is your next item?')
print(shoppingList)

You can add some print statements to explain how to use your system (to make it more user-friendly)

Zac S
  • 49
  • 1
  • 9