매서드 호출
입금 횟수가 5회가 될 때, 잔고를 기준으로 1%의 이자가 잔고에 추가되도록 하라.
import random
call = 0
class Account:
account_count = 0
def __init__(self, name, balance):
#self.deposit_count = 0
self.name = name
self.balance = balance
self.bank = "SC은행"
num1 = random.randint(0, 999)
num2 = random.randint(0, 99)
num3 = random.randint(0, 999999)
num1 = str(num1).zfill(3)
num2 = str(num2).zfill(2)
num3 = str(num3).zfill(6)
self.number = num1 + '-' + num2 + '-' + num3
Account.account_count +=1
def deposit(self, amount):
if amount >= 1:
self.balance += amount
global call
call += 1 #self.deposit_count += 1
if call % 5 == 0: #self.deposit_count % 5 == 0:
self.balance = (self.balance) * 1.01
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
def display_info(self):
print("은행이름:", self.bank)
print("예금주:", self.name)
print("계좌번호:", self.number)
print("잔액:", format(self.balance, ','))
@classmethod
def get_account_num(cls):
print(cls.account_count)
kim = Account("김은혜", 100)
kim.deposit(10)
kim.deposit(10)
kim.deposit(10)
kim.deposit(10)
kim.deposit(10)
kim.deposit(10)
print(kim.balance)
print(call)
deposit 메서드에 if문을 추가하고, deposit을 호출한 횟수를 저장할 새로운 변수와 변수의 초기 조건을 만들어야 하는데, global 변수와 nonlocal 변수를 사용하는 줄 알고, class 밖에 변수를 만들고, 메서드에서 class 밖의 변수를 가져오는 방법으로 문제를 풀었는데,
해답에서는 init 에 초기값을 생성하는 것처럼 변수를 만들어 주고 deposit 메서드에서 self.변수로 사용하였다.
280번 입출금 기록을 출력하는 메서드를 만드는 것도 나는 nonlocal 변수를 사용하기 위해 함수 안에 함수를 만들었는데, 이 때의 문제는, 기록을 출력할 수 있는 메서드만을 호출 할 수 없다는 것이었다.(?)(입금/출금을 하면 기록이 나오는 식,,,) 그래서 위 문제처럼 global 변수를 쓰고(class 밖에 만들어 놓은 빈 리스트), 기록을 출력하는 메서드를 만들었는데, 결국 해답은 init 에 입출금을 기록할 빈 리스트를 만들고, 메서드를 따로 만들어 주었다.