Skip to content

Commit bbee34f

Browse files
committed
Added - ProblemSet6/SentimentalCash
1 parent b371e8f commit bbee34f

File tree

1 file changed

+44
-0
lines changed
  • Week 6 - Python/ProblemSet6/SentimentalCash

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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()

0 commit comments

Comments
 (0)