다음 예에서는 오류가 발생합니다.
“local variable 'monthlyPayment' referenced before assignment”
다음은 내 코드입니다.
def getMonthlyPayment(self, annualInterestRate, loanAmount, numberOfYears, loan):
if loan == 0 and (annualInterestRate * 1 - loanAmount) >= 2 and numberOfYears <= 3:
monthlyPayment = min((annualInterestRate * 1 - loanAmount), 10)
return monthlyPayment
elif loan == 1 and (annualInterestRate * 2 - loanAmount) >= 2 and numberOfYears <= 3:
monthlyPayment = min((annualInterestRate * 2 - loanAmount), 20)
return monthlyPayment
elif loan > 1 and (annualInterestRate * 4 - loanAmount) >= 2 and numberOfYears <= 3:
monthlyPayment = min((annualInterestRate * 4 - loanAmount), 20)
return monthlyPayment
else:
monthlyPayment = 'Not Qualify'
return monthlyPayment
여러 else if 문에 대한 적절한 구조는 다음과 같습니다.
if (condition):
monthlypayment = something
elif (condition2):
monthlypayment = something2
elif (condition3):
monthlypayment = something3
else:
monthlypayment = something4
따라서 if 문이 올바르게 형식화되면 월별 지불에 한 번만 값이 할당됩니다. 위의 @anonymous의 주석에 따라 다음으로 함수를 종료 할 수 있습니다.return monthlypayment
이 함수가 self 또는 @staticmethod 데코레이터가 필요한 클래스 정의의 일부라고 가정합니다. 객체 지향이 아닌 코드를 작성하는 경우 자아를 없앨 수 있습니다.
def getMonthlyPayment(self,annualInterestRate,loanAmount,numberOfYears,loan):
if loan == 0 and (annualInterestRate * 1 - loanAmount) >= 2 and numberOfYears <= 3:
monthlyPayment = min((annualInterestRate * 1 - loanAmount), 10)
elif loan == 1 and (annualInterestRate * 2 - loanAmount) >= 2 and numberOfYears <= 3:
monthlyPayment = min((annualInterestRate * 2 - loanAmount), 20)
elif loan > 1 and (annualInterestRate * 4 - loanAmount) >= 2 and numberOfYears <= 3:
monthlyPayment = min((annualInterestRate * 4 - loanAmount), 20)
else:
monthlyPayment='Not Qualify'
return monthlyPayment
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다