Python Forum
How to Think Like a Computer Scientist - Iteration Problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to Think Like a Computer Scientist - Iteration Problem (/thread-4770.html)



How to Think Like a Computer Scientist - Iteration Problem - rubicana - Sep-07-2017

Please see http://interactivepython.org/runestone/static/thinkcspy/MoreAboutIteration/SentinelValuesAndValidation.html#sentinel-values

This is the code given.. (This might remove the idents - see the website if this happens)

def checkout():
total = 0
count = 0
moreItems = True
while moreItems:
price = float(input('Enter price of item (0 when done): '))
if price != 0:
count = count + 1
total = total + price
print('Subtotal: $', total)
else:
moreItems = False
average = total / count
print('Total items:', count)
print('Total $', total)
print('Average price per item: $', average)

checkout()

On the site underneath the code it states

"If you enter zero the first time you are asked for a price, the loop will end, and the program will try to divide by zero. Use an if/else statement outside the loop to avoid the division by zero and tell the user that you can’t compute an average without data."

Haven't been able to work this out - a seemingly simple if/else statement outside of the loop to catch the divide by zero. Any help gratefully received. Many thanks Tom


RE: How to Think Like a Computer Scientist - Iteration Problem - Larz60+ - Sep-07-2017

Please follow forum rules:
  • Post code between code tags, see BBCODE
  • To preserve indentation, post using shift-ctrl-v



RE: How to Think Like a Computer Scientist - Iteration Problem - rubicana - Sep-07-2017

Code from here

def checkout():
    total = 0
    count = 0
    moreItems = True
    while moreItems:
        price = float(input('Enter price of item (0 when done): '))
        if price != 0:
            count = count + 1
            total = total + price
            print('Subtotal: $', total)
        else:
            moreItems = False
    average = total / count
    print('Total items:', count)
    print('Total $', total)
    print('Average price per item: $', average)

checkout()
On the site underneath the code it states

"If you enter zero the first time you are asked for a price, the loop will end, and the program will try to divide by zero. Use an if/else statement outside the loop to avoid the division by zero and tell the user that you can’t compute an average without data."

Haven't been able to work this out - a seemingly simple if/else statement outside of the loop to catch the divide by zero. Any help gratefully received. Many thanks Tom


RE: How to Think Like a Computer Scientist - Iteration Problem - ichabod801 - Sep-07-2017

That's just the code from the website. What have you tried in terms of the if/else statement, and how is it not working?


RE: How to Think Like a Computer Scientist - Iteration Problem - rubicana - Sep-07-2017

This is where I have got to. It does work to the extent of returning a message and ending the code if an initial 0 is entered. Any other initial value is ignored which isn't great (although you could argue it still works). Any help with fine tuning gratefully received.

def checkout():
    total = 0
    count = 0
    moreItems = True
    while moreItems:
        price = float(input('Enter price of item (0 when done): '))
        if price > 0:
            count = count + 1
            total = total + price
            print('Subtotal: £', total)
        elif price < 0:
            print('Negative value - price still £',total)
        else:
            moreItems = False
    average = total / count
    print('Total items:', count)
    print('Total £', total)
    print('Average price per item: £', average)

price = float(input('Enter price of item (0 when done): '))
if price == 0:
    print('Basket empty - Please rerun')
else:
    checkout()



RE: How to Think Like a Computer Scientist - Iteration Problem - ichabod801 - Sep-07-2017

The division by zero is causing the problem. You want the if statement in checkout, right before the division, to prevent the problem from ever happening.


RE: How to Think Like a Computer Scientist - Iteration Problem - probot - Feb-03-2018

This seems to be working for me:

def checkout():
    total = 0
    count = 0
    moreItems = True
    while moreItems:
        price = float(input('Enter price of item (0 when done): '))
        if price > 0:
            count = count + 1
            total = total + price
            print('Subtotal: £', total)
        elif price < 0:
            print('Negative value - price still £', total)
        else:
            moreItems = False
    if total == 0:
        print('Basket empty - Please rerun')
    else:
        average = total / count
        print('Total items:', count)
        print('Total £', total)
        print('Average price per item: £', average)


checkout()