-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcash.py
48 lines (33 loc) · 1.13 KB
/
cash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
https://cs50.harvard.edu/x/2023/psets/6/cash/
"""
def main():
dollars = get_input()
amount = get_coins(dollars)
print(amount)
def get_input():
"""Prompts user for a positive float. Returns that amount times 100 as an integer."""
while True:
try:
dollars = float(input("Change owed: "))
if dollars > 0:
return int(dollars * 100)
except ValueError as error:
print(error)
def get_coins(dollars):
"""Returns the smallest number of coins that can be given for the change owed."""
counter = 0
# Loop through a list that contains all coins available.
for coin in [25, 10, 5, 1]:
# Divide the dollars by each coin and store integer result in a temporary variable.
amount = int(dollars / coin)
# Substract from dollars that result multiplied by the coin.
dollars -= amount * coin
# Add to counter the result of the division.
counter += amount
# Break out of the loop when dollars reach 0.
if dollars == 0:
break
return counter
if __name__ == "__main__":
main()