python - 递归调用方法变量python
问题描述
我正在尝试生成一个从方法变量获取输入以递归方式填充的列表。(我相信)
我的代码:
class Register:
cur_unit = [100, 50, 20, 10, 5, 1, .25, .10, .05, .01]
reg_amount = []
def load_reg(self):
self.reg_amount = float(input('Enter amount of money in register...\n'))
def transaction(self):
trans_amount = float(input('Enter the cost of the transaction...\n'))
if trans_amount > self.reg_amount:
print("I'm sorry, but we don't have enough money in the register to allow this transaction...\n")
else:
cash_paid = float(input('Enter how much money you will pay with...\n'))
change_due = cash_paid - trans_amount
new_reg_amount = self.reg_amount - change_due
if new_reg_amount < 0:
print("I'm sorry, but we don't have enough money in the register to allow this transaction...\n")
else:
new_reg_amount = round(new_reg_amount, 2)
change_due = round(change_due, 2)
print('\n' + str(new_reg_amount))
print(change_due)
for i in self.cur_unit:
if change_due - i >= 0:
return [i] + [cash_paid - i]
reg = Register()
reg.load_reg()
res = reg.transaction()
print(res)
产生不良结果的结果:
Enter amount of money in register...
200
Enter the cost of the transaction...
24.24
Enter how much money you will pay with...
50
174.24
25.76
[20, 30.0] # undesired result
Process finished with exit code 0
如果可以从 cash_paid 中减去单位,而 change_due 不等于或小于 0,则期望的结果将贯穿 cur_unit 并带回每个单位:(这是需要的更多详细信息)
[20, 5, .25, .25, .25, .01]
解决方案
正如 Prune 在评论中指出的那样,这个问题通过迭代比递归更好地解决。万一你好奇,我写了两种方法:split_change_r
它是一个递归函数,split_change
是一个更清晰的迭代解决方案。请注意,他们假设您的cur_unit
列表已经排序。
class Register:
cur_unit = [100, 50, 20, 10, 5, 1, .25, .10, .05, .01]
reg_amount = []
def load_reg(self):
self.reg_amount = float(input('Enter amount of money in register...\n'))
def split_change_r(self, amount, l = []):
next_cur = [a for a in self.cur_unit if a <= amount][0]
if next_cur == amount:
return l + [next_cur]
else:
# here is the recursive call
return self.split_change_r(round(amount - next_cur, 2), l + [next_cur])
def split_change(self, amount):
r = []
while(amount != 0):
next_cur = [a for a in self.cur_unit if a <= amount][0]
amount = round(amount - next_cur, 2)
r.append(next_cur)
return r
def transaction(self):
trans_amount = float(input('Enter the cost of the transaction...\n'))
if trans_amount > self.reg_amount:
print("I'm sorry, but we don't have enough money in the register to allow this transaction...\n")
else:
cash_paid = float(input('Enter how much money you will pay with...\n'))
change_due = cash_paid - trans_amount
new_reg_amount = self.reg_amount - change_due
if new_reg_amount < 0:
print("I'm sorry, but we don't have enough money in the register to allow this transaction...\n")
else:
new_reg_amount = round(new_reg_amount, 2)
change_due = round(change_due, 2)
print('\n' + str(new_reg_amount))
print(change_due)
return self.split_change(change_due)
reg = Register()
reg.load_reg()
res = reg.transaction()
print(res)
示例输出:
Enter amount of money in register...
200
Enter the cost of the transaction...
24.24
Enter how much money you will pay with...
50
174.24
25.76
[20, 5, 0.25, 0.25, 0.25, 0.01]
推荐阅读
- firebase - Firebase 实时数据库协议开销和 SSL 加密开销
- excel - 超级简单的 VLOOKUP 无法按预期检索值
- php - 为什么使用 CK 编辑器粘贴图表,当从页面查看时,它不能很好地显示?
- php - Angular:使用 api 数据填充的材料自动完成返回 JSON 值
- php - 如何优化此方法并避免 foreach 循环
- networking - 在我的 Ubuntu 17.04 系统中,为什么 /etc/resolv.conf fie 在重新启动时被覆盖?
- vb.net - 如何在 vb.net 中查找值为“新行”的 Excel 范围的地址?
- selenium - 数据提供者不匹配
- angular - 无法绑定到“ngForOf”,因为它不是组件的已知属性
- bash - 在 git 中,我如何以编程方式从历史记录(变基)中删除/删除特定提交?