By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

appending user entered items to array, then displaying them

P: 3
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!

Expand|Select|Wrap|Line Numbers
  1. ShopList = [] 
  2. ListItem= ()
  3. print("To make a shopping list")
  4. print("Enter the items, when you have finished press 0 to display the list.")
  5. while ListItem != 0:
  6.     ListItem = input ("Enter your Item to the List: ")
  7.     ShopList.append(ListItem)
  8. print ("Here's your Shopping List:")
  9. for item in ShopList:
  10.     print item
Jun 19 '14 #1

✓ answered by dwblas

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.
Expand|Select|Wrap|Line Numbers
  1. while list_item != "0":  ## Note the quotes
  2.      list_item = input ("Enter your Item to the List: ")
  3.      print(type(list_item)) 

Share this Question
Share on Google+
3 Replies


Expert 100+
P: 619
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.
Expand|Select|Wrap|Line Numbers
  1. while list_item != "0":  ## Note the quotes
  2.      list_item = input ("Enter your Item to the List: ")
  3.      print(type(list_item)) 
Jun 19 '14 #2

P: 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!
Jun 19 '14 #3

Expert 100+
P: 619
The best way to start is with one of the tutorials https://wiki.python.org/moin/BeginnersGuide/Programmers
Jun 20 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.