(New to Python) Shopping List Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ferzan68
    New Member
    • Oct 2019
    • 1

    (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.



    Code:
    total = 0                                       
    while True:                                     
                                             
      print('Cost of item')                                     
                                             
      item = input()                                        
                                             
      if item != -1:                                        
        total = total + item                                        
      if item == -1:                                        
                                             
          break                                     
                                             
    print(total)
    Last edited by gits; Oct 29 '19, 07:24 AM. Reason: added code tags
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    Welcome to Bytes!

    You can use a test expression with while loop to achieve such. For e.g.
    Code:
    ...
    i=1
    while i<11:
      i += 1
      ...
    ...

    Comment

    Working...