473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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!

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)) 

3 1336
dwblas
626 Expert 512MB
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
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
dwblas
626 Expert 512MB
The best way to start is with one of the tutorials https://wiki.python.org/moin/BeginnersGuide/Programmers
Jun 20 '14 #4

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

Similar topics

4
by: Gregory | last post by:
Hello, I've managed to build two web pages, one that can display images with associated text data in a table, and one that can resize and display images without the text. I'd like to resize the...
13
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
0
by: emer.kurbegovic | last post by:
I need to build a filter that will filter user entered html and which will allow only certain html tags through (i.e. <IMG>, <SCRIPTand <EMBEDwould be allowed). i was going to HtmlEncode the...
0
by: tenko | last post by:
here's a question regarding changing units, say from g to kg without changing the entire codes.... my mathematical equation uses KG (kilograms), but the user can choose whether to enter values in...
9
by: Anneybo | last post by:
Alright, I give up! I'm asking the experts. I have created a database that calculates PTO for employees. I need to be able to cache the report by user entered dates and specific employee names. I...
0
by: dgs | last post by:
How to scan the user-entered value ? like, we have "scanf()" in 'C'.......
8
by: unkietee | last post by:
Hi Experts, my first post here! I hope you can help me. I have a basic form which I want the user to enter either a number (postcode/zipcode) or text (suburb). When they hit submit I want to...
4
by: ghjk | last post by:
I want to compare user entered date with data base datetime value. Database value in datetime type and first i want to extract date part and then compare with user entered value. This is my query....
2
by: gilly1471 | last post by:
Yep, new to perl, I started yesterday, but am quickly excelling. I would just like to know how to use user entered data. Let's say I have a programm that calculates the circumference of a circle....
3
by: rhonda2010 | last post by:
My project consists of a user interface designed in Visual Studio 2010 Express. I have it linked to an Access database. At this time, I have two text boxes that the user will enter a number. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.