473,382 Members | 1,646 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,382 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 1334
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.