找钱计算源代码
时间: 2023-08-11 19:03:38 浏览: 67
以下是一个简单的找钱计算程序的 Python 源代码:
```python
def calculate_change(price, paid):
change = paid - price
if change < 0:
print("Error: Paid amount is less than the price.")
else:
print("Change:", change)
denominations = [100, 50, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01]
for denomination in denominations:
count = int(change / denomination)
if count > 0:
print(int(denomination * 100), "cents:", count)
change -= count * denomination
# 示例用法
calculate_change(12.50, 20.00)
```
该代码定义了一个名为 `calculate_change()` 的函数,该函数接受两个参数:商品价格 `price` 和顾客支付的金额 `paid`。函数会计算出找零金额,并输出每种面额的货币需要找多少张/个。
该程序假设货币的面额为 100 美分、50 美分、20 美分、10 美分、5 美分、1 美分、25 美分、10 美分、5 美分和 1 美分。如果需要使用其他面额,可以修改 `denominations` 列表变量。
示例用法 `calculate_change(12.50, 20.00)` 会计算出 12.50 美元的商品的找零金额,并输出结果。