File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Week 6 - Python/ProblemSet6/SentimentalCash Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ def main ():
2+ dollars = get_input ()
3+ amount = get_coins (dollars )
4+
5+ print (amount )
6+
7+
8+ def get_input ():
9+ """Prompts user for a positive float. Returns that amount times 100 as an integer."""
10+
11+ while True :
12+ try :
13+ dollars = float (input ("Change owed: " ))
14+
15+ if dollars > 0 :
16+ return int (dollars * 100 )
17+
18+ except ValueError as error :
19+ print (error )
20+
21+
22+ def get_coins (dollars ):
23+ """Returns the smallest number of coins that can be given for the change owed."""
24+
25+ counter = 0
26+
27+ # Loop through a list that contains all coins available.
28+ for coin in [25 , 10 , 5 , 1 ]:
29+ # Divide the dollars by each coin and store integer result in a temporary variable.
30+ amount = int (dollars / coin )
31+ # Substract from dollars that result multiplied by the coin.
32+ dollars -= amount * coin
33+ # Add to counter the result of the division.
34+ counter += amount
35+
36+ # Break out of the loop when dollars reach 0.
37+ if dollars == 0 :
38+ break
39+
40+ return counter
41+
42+
43+ if __name__ == "__main__" :
44+ main ()
You can’t perform that action at this time.
0 commit comments