-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccumulate.py
More file actions
24 lines (20 loc) · 918 Bytes
/
accumulate.py
File metadata and controls
24 lines (20 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import itertools
"""Accumulate is a function that provides a running total of the values in an iterable. The last value is the sum total.
Prior to that sum total, each value represents the sum of all previous values. One obvious application would be to use
in a GUI cash register as demonstrated in the console version below."""
prices = []
first = input("Please enter the first price: ")
prices.append(float(first))
while True:
num = input('Please enter the next price (enter "x" to stop): ')
if str(num) == 'x'.lower():
break
else:
num = float(num)
prices.append(num)
print(f"${num:,.2f}")
run_tot = list(itertools.accumulate(prices))[-1]
print(f"\tTotal: ${run_tot:,.2f}")
c = len(prices)
print(f"\nThe Grand Total of {c} items is: ${run_tot:,.2f}.")
print(f"The sum of the first two items is: ${list(itertools.accumulate(prices))[1]:,.2f}.")