I want to use the updated variable in the function outside the function.

Asked 2 years ago, Updated 2 years ago, 16 views

Prerequisites/What you want to achieve
I am creating a program in python that asks for the repayment period of the debt.
As a practice, I was going to make the following program using class.
I wanted to use the variable that I changed in the function outside the function, but I couldn't use it. The value in the variable is not updated.Please tell me what's wrong.

debt=int('Enter your debt(yen)'))
Interest= float(input('annual rate (%)>')
Repayment_amount=int(input('Monthly Repayment'))
total = 0
month = 0

while debt > Repayment_amount:
    debt=debt* (1+ Interest/12/100)
    debt-=Repayment_amount
    total+=Repayment_amount
    month + = 1
    print(month, 'month: repayment amount', Repayment_amount, 'yen', 'remaining', debt)

debt=debt* (1+ Interest/12/100)
all=int(total+debt)
debt=int(debt)
print(month+1, 'month: repayment amount', str(debt), 'yen paid off with this.Total repayment amount: ', str(all), 'yen')

This is the program that doesn't update well↓

class Calculation():  
    def__init__(self, debit, Repayment_amount, Interest, total, month):
        self.debt =debt
        self. Interest = Interest
        self.Repayment_amount =Repayment_amount
        self.total = total 
        self.month=month


    def tukiduki (self, debt, Repayment_amount, Interest, total, month):
        while debt > Repayment_amount:
            
            debt=debt* (1+ Interest/12/100)
            debt-=Repayment_amount
            total = total + Repayment_amount
            month = month+1
            print(month, 'month: repayment amount', Repayment_amount, 'yen', 'remaining', debt)
            
debt=int(input('Enter your debt(yen)'))
Interest= float(input('annual rate (%)>')
Repayment_amount=int(input('Monthly Repayment'))
total = 0
month = 0

# create instance variables
c1 = Calculation (debt, Repayment_amount, Interest, total, month)

#repeat one's monthly debt payments
c1.tukiduki(debt, Repayment_amount, Interest, total, month)

# Calculation of the amount of repayment for the last month
c=c1.debt*(1+c1.Inest/12/100)
# ask for the total amount of repayment
all=int(c1.total+c)
# convert the last month's repayment to an integer
c=int(c)
# Outputs the amount of repayment and total amount of repayment for the last month
print(c1.month+1, 'month: repayment amount', str(c), 'yen paid off with this.Total repayment amount: ', str(all), 'yen')

Tried
I thought it would be good to substitute the updated value for the instance variable, so I changed the argument of the tukiduki method to an instance variable, and I tried to substitute the variable in the method to repeat it, but it didn't solve and I don't know how to do it.
I looked into the instance variable, but I couldn't find a way to use the updated content in the while statement outside the class.
Attempting to return the updated value in the while statement did not repeat.
If you don't mind, please let me know.

Supplementary Information (for example, FW/Tool Version)
macOS BigSur version 11.2.3
python 3.9.5
Editor VS code 1.58.1

python

2022-09-30 15:31

1 Answers

I solved myself!
Sorry for the viewers!

class Calculation():  
    def__init__(self, debit, Repayment_amount, Interest, total, month):
        self.debt =debt
        self. Interest = Interest
        self.Repayment_amount =Repayment_amount
        self.total = total 
        self.month=month

    deftukiduki(self,):
        while self.debt>self.Repayment_amount:
            
            self.debt = self.debt* (1+self.Inest/12/100)
            self.debt-=self.Repayment_amount
            self.total = self.total + self.Repayment_amount
            self.month=self.month+1
            print(self.month, 'month: repayment amount', self.Repayment_amount, 'yen', 'remaining', self.debt)
            
debt=int(input('Enter your debt(yen)'))
Interest= float(input('annual rate (%)>')
Repayment_amount=int(input('Monthly Repayment'))
total = 0
month = 0

# create instance variables
c1 = Calculation (debt, Repayment_amount, Interest, total, month)

#repeat one's monthly debt payments
c1.tukiduki()

# Calculation of the amount of repayment for the last month
c=c1.debt*(1+c1.Inest/12/100)
# ask for the total amount of repayment
all=int(c1.total+c)
# convert the last month's repayment to an integer
c=int(c)
# Outputs the amount of repayment and total amount of repayment for the last month
print(c1.month+1, 'month: repayment amount', str(c), 'yen paid off with this.Total repayment amount: ', str(all), 'yen')


2022-09-30 15:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.