473,387 Members | 1,463 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,387 software developers and data experts.

(New to Python) Shopping List Code

How can I stop this code when -1 is typed or at a maximum item count of ten.
At the moment the code seems to be in a infinite loop meaning it keeps on asking for an entry until -1 is typed.



Expand|Select|Wrap|Line Numbers
  1. total = 0                                       
  2. while True:                                     
  3.  
  4.   print('Cost of item')                                     
  5.  
  6.   item = input()                                        
  7.  
  8.   if item != -1:                                        
  9.     total = total + item                                        
  10.   if item == -1:                                        
  11.  
  12.       break                                     
  13.  
  14. print(total)
Oct 28 '19 #1
1 1724
dev7060
636 Expert 512MB
Welcome to Bytes!

You can use a test expression with while loop to achieve such. For e.g.
Expand|Select|Wrap|Line Numbers
  1. ...
  2. i=1
  3. while i<11:
  4.   i += 1
  5.   ...
  6. ...
  7.  
Oct 28 '19 #2

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

Similar topics

0
by: CPD | last post by:
I'm looking for some open source shopping cart code that will support discount coupons and can also be customized for charitable donations. PayPal has something similar, but it is not flexible...
14
by: Uwe Mayer | last post by:
Hi, I know the python community is not very receptive towards extending the python syntax. Nevertheless I'd like to make a suggestion and hear your pro and cons. I want so suggest a...
7
by: stormslayer | last post by:
Folks: I've been considering a shift to python. I currently use c++builder (borland) or perl. I do computational models / statistics programming, and was interested in python b/c it a. has...
26
by: Mark Hahn | last post by:
This is an announcement of the beginning of development of a new Python-like language called PyCs (pronounced "pie-cees"). Like IronPython, PyCs will be Python on .Net but it will have more...
38
by: looping | last post by:
For Python developers around. >From Python 2.5 doc: The list of base classes in a class definition can now be empty. As an example, this is now legal: class C(): pass nice but why this...
1
by: Kenneth McDonald | last post by:
Would a mailing list and newsgroup for "python contributions" be of interest? I currently have a module which is built on top of, and is intended to semantically replace, the 're' module. I use it...
2
by: JRoughgarden | last post by:
After a customer decides to buy a shopping list, there is generally a need to store/insert one master record and a variable number of child detail records, preferably all wrapped in a transaction....
4
by: belfallas | last post by:
Hi, ive got this problem with one of my databeses. My question is, how do i create a sort of a shopping list on access? Im doing a ficitional pizza shop, and what i want to do is to have an...
2
ironmonkey69
by: ironmonkey69 | last post by:
I need help to get this script to accept more than one number in the 'elem = ' line so that I will be able to zero more than one line at a time. Right now I am able to get it to do one line at a...
0
by: service0043 | last post by:
Some bargain prices can be found on jewelry that is sold in bulk and at auction houses on the internet. While the condition of the items might be used, some fine jewelry pieces will be sold as new....
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.