第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

遞歸調(diào)用方法變量 python

遞歸調(diào)用方法變量 python

牧羊人nacy 2022-09-27 16:17:54
我正在嘗試生成一個以遞歸方式填充的列表,該列表從方法變量中獲取輸入。(我相信)我的代碼: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)產(chǎn)生不良結(jié)果的結(jié)果: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, 30.0] # undesired resultProcess finished with exit code 0期望的結(jié)果,它將貫穿cur_unit,如果可以從cash_paid中減去單位而不change_due等于或小于0,則每個單位都會返回:(這是為了需要更多細節(jié))[20, 5, .25, .25, .25, .01]
查看完整描述

1 回答

?
慕姐4208626

TA貢獻1852條經(jīng)驗 獲得超7個贊

正如Prune在評論中指出的那樣,通過迭代比使用遞歸更好地解決了這個問題。我寫了一些方法,以防萬一你好奇:是一個遞歸函數(shù),是一個更干凈的迭代解決方案。請注意,他們假設(shè)您的列表已排序。split_change_rsplit_changecur_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]


查看完整回答
反對 回復(fù) 2022-09-27
  • 1 回答
  • 0 關(guān)注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號