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

(New to Python) Shopping List Code

P: 1
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
Share this Question
Share on Google+
1 Reply


100+
P: 144
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

Post your reply

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