appending user entered items to array, then displaying them

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackpaddycat
    New Member
    • Jun 2014
    • 3

    appending user entered items to array, then displaying them

    OK, so I am trying to make a shopping list which appends each item entered to an array until the user enters "0"
    I have got this far and it just doesnt work :-(
    Would really appreciate some suggestions or pointers!
    Thanks very much!

    Code:
    ShopList = [] 
    ListItem= ()
    print("To make a shopping list")
    print("Enter the items, when you have finished press 0 to display the list.")
    while ListItem != 0:
        ListItem = input ("Enter your Item to the List: ")
        ShopList.append(ListItem)
    print ("Here's your Shopping List:")
    for item in ShopList:
        print item
    Last edited by Rabbit; Jun 19 '14, 03:49 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    The answer depends on whether you are using Python 2.x or 3.x. Your program will work in 2.x but will not in 3.x because of a difference in way the input() function is programmed. Print the type of the item entered after the input statement. If the type is string then comparing list_item to the integer zero will never be true because they are different types. So the answer to this overly long explanation is if you are using Python 3, and the use of print as a function suggests you are, then compare to "0", a string, instead of 0 an integer. Also, note that the Python Style Guide prefers variable names that are all lower case with underlines. CamelCase is for classes. This convention helps others read and understand your code, especially when it gets more complex.
    Code:
    while list_item != "0":  ## Note the quotes
         list_item = input ("Enter your Item to the List: ")
         print(type(list_item))

    Comment

    • blackpaddycat
      New Member
      • Jun 2014
      • 3

      #3
      Ah, Thanks very much dwblas, and also for the reference to the style guide as well. V new to python, and indeed programming.
      Have now got the code working for 2.7 and 3.3.
      Feeling pleased rather than frustrated. Not very good at this programming malarky!

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        The best way to start is with one of the tutorials https://wiki.python.org/moin/BeginnersGuide/Programmers

        Comment

        Working...