Я пытаюсь создать список, который заполняется рекурсивно, принимая данные из переменной метода. (Я верю)
Мой код:
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]