Sep-07-2017, 11:43 AM
Please see http://interactivepython.org/runestone/s...nel-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
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