猿问

递归调用方法变量 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

期望的结果,它将贯穿cur_unit,如果可以从cash_paid中减去单位而不change_due等于或小于0,则每个单位都会返回:(这是为了需要更多细节)


[20, 5, .25, .25, .25, .01]


牧羊人nacy
浏览 147回答 1
1回答

慕姐4208626

正如Prune在评论中指出的那样,通过迭代比使用递归更好地解决了这个问题。我写了一些方法,以防万一你好奇:是一个递归函数,是一个更干净的迭代解决方案。请注意,他们假设您的列表已排序。split_change_rsplit_changecur_unitclass Register:&nbsp; &nbsp; cur_unit = [100, 50, 20, 10, 5, 1, .25, .10, .05, .01]&nbsp; &nbsp; reg_amount = []&nbsp; &nbsp; def load_reg(self):&nbsp; &nbsp; &nbsp; &nbsp; self.reg_amount = float(input('Enter amount of money in register...\n'))&nbsp; &nbsp; def split_change_r(self, amount, l = []):&nbsp; &nbsp; &nbsp; &nbsp; next_cur = [a for a in self.cur_unit if a <= amount][0]&nbsp; &nbsp; &nbsp; &nbsp; if next_cur == amount:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return l + [next_cur]&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # here is the recursive call&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return self.split_change_r(round(amount - next_cur, 2), l + [next_cur])&nbsp; &nbsp; def split_change(self, amount):&nbsp; &nbsp; &nbsp; &nbsp; r = []&nbsp; &nbsp; &nbsp; &nbsp; while(amount != 0):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next_cur = [a for a in self.cur_unit if a <= amount][0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; amount = round(amount - next_cur, 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r.append(next_cur)&nbsp; &nbsp; &nbsp; &nbsp; return r&nbsp; &nbsp; def transaction(self):&nbsp; &nbsp; &nbsp; &nbsp; trans_amount = float(input('Enter the cost of the transaction...\n'))&nbsp; &nbsp; &nbsp; &nbsp; if trans_amount > self.reg_amount:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("I'm sorry, but we don't have enough money in the register to allow this transaction...\n")&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cash_paid = float(input('Enter how much money you will pay with...\n'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; change_due = cash_paid - trans_amount&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_reg_amount = self.reg_amount - change_due&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if new_reg_amount < 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("I'm sorry, but we don't have enough money in the register to allow this transaction...\n")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_reg_amount = round(new_reg_amount, 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; change_due = round(change_due, 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('\n' + str(new_reg_amount))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(change_due)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return self.split_change(change_due)reg = Register()reg.load_reg()res = reg.transaction()print(res)输出示例:Enter amount of money in register...200Enter the cost of the transaction...24.24Enter how much money you will pay with...50174.2425.76[20, 5, 0.25, 0.25, 0.25, 0.01]
随时随地看视频慕课网APP

相关分类

Python
我要回答