728x90
1. Account 클래스에 입금을 위한 deposit 메서드를 추가하세요. 입금은 최소 1원 이상만 가능합니다.
정답확인 ->
import random
class Account:
# class variable
account_count = 0
def __init__(self, name, balance):
self.name = name
self.balance = balance
self.bank = "SC은행"
# 3-2-6
num1 = random.randint(0, 999)
num2 = random.randint(0, 99)
num3 = random.randint(0, 999999)
num1 = str(num1).zfill(3) # 1 -> '1' -> '001'
num2 = str(num2).zfill(2) # 1 -> '1' -> '01'
num3 = str(num3).zfill(6) # 1 -> '1' -> '0000001'
self.account_number = num1 + '-' + num2 + '-' + num3 # 001-01-000001
Account.account_count +=1
@classmethod
def get_account_num(cls):
print(cls.account_count) # Account.account_count
def deposit(self, amount):
if amount >= 1:
self.balance += amount
2. Account 클래스에 출금을 위한 withdraw 메서드를 추가하세요. 출금은 계좌의 잔고 이상으로 출금할 수는 없습니다.
정답확인 ->
import random
class Account:
# class variable
account_count = 0
def __init__(self, name, balance):
self.name = name
self.balance = balance
self.bank = "SC은행"
# 3-2-6
num1 = random.randint(0, 999)
num2 = random.randint(0, 99)
num3 = random.randint(0, 999999)
num1 = str(num1).zfill(3) # 1 -> '1' -> '001'
num2 = str(num2).zfill(2) # 1 -> '1' -> '01'
num3 = str(num3).zfill(6) # 1 -> '1' -> '0000001'
self.account_number = num1 + '-' + num2 + '-' + num3 # 001-01-000001
Account.account_count += 1
@classmethod
def get_account_num(cls):
print(cls.account_count) # Account.account_count
def deposit(self, amount):
if amount >= 1:
self.balance += amount
def withdraw(self, amount):
if self.balance > amount:
self.balance -= amount
k = Account("kim", 100)
k.deposit(100)
k.withdraw(90)
print(k.balance)
3. Account 인스턴스에 저장된 정보를 출력하는 display_info() 메서드를 추가하세요. 잔고는 세자리마다 쉼표를 출력하세요.
은행이름: SC은행
예금주: 파이썬
계좌번호: 111-11-111111
잔고: 10,000원
정답확인 ->
import random
class Account:
# class variable
account_count = 0
def __init__(self, name, balance):
self.name = name
self.balance = balance
self.bank = "SC은행"
# 3-2-6
num1 = random.randint(0, 999)
num2 = random.randint(0, 99)
num3 = random.randint(0, 999999)
num1 = str(num1).zfill(3) # 1 -> '1' -> '001'
num2 = str(num2).zfill(2) # 1 -> '1' -> '01'
num3 = str(num3).zfill(6) # 1 -> '1' -> '0000001'
self.account_number = num1 + '-' + num2 + '-' + num3 # 001-01-000001
Account.account_count += 1
@classmethod
def get_account_num(cls):
print(cls.account_count) # Account.account_count
def deposit(self, amount):
if amount >= 1:
self.balance += amount
def withdraw(self, amount):
if self.balance > amount:
self.balance -= amount
def display_info(self):
print("은행이름: ", self.bank)
print("예금주: ", self.name)
print("계좌번호: ", self.account_number)
print("잔고: ", self.balance)
p = Account("파이썬", 10000)
p.display_info()
4. 입금 횟수가 5회가 될 때 잔고를 기준으로 1%의 이자가 잔고에 추가되도록 코드를 변경해보세요.
정답확인 ->
import random
class Account:
# class variable
account_count = 0
def __init__(self, name, balance):
self.deposit_count = 0
self.name = name
self.balance = balance
self.bank = "SC은행"
# 3-2-6
num1 = random.randint(0, 999)
num2 = random.randint(0, 99)
num3 = random.randint(0, 999999)
num1 = str(num1).zfill(3) # 1 -> '1' -> '001'
num2 = str(num2).zfill(2) # 1 -> '1' -> '01'
num3 = str(num3).zfill(6) # 1 -> '1' -> '0000001'
self.account_number = num1 + '-' + num2 + '-' + num3 # 001-01-000001
Account.account_count += 1
@classmethod
def get_account_num(cls):
print(cls.account_count) # Account.account_count
def deposit(self, amount):
if amount >= 1:
self.balance += amount
self.deposit_count += 1
if self.deposit_count % 5 == 0: # 5, 10, 15
# 이자 지금
self.balance = (self.balance * 1.01)
def withdraw(self, amount):
if self.balance > amount:
self.balance -= amount
def display_info(self):
print("은행이름: ", self.bank)
print("예금주: ", self.name)
print("계좌번호: ", self.account_number)
print("잔고: ", self.balance)
p = Account("파이썬", 10000)
p.deposit(10000)
p.deposit(10000)
p.deposit(10000)
p.deposit(5000)
p.deposit(5000)
print(p.balance)
728x90
'알고리즘' 카테고리의 다른 글
파이썬 클래스 예제 1-8 (0) | 2022.04.25 |
---|---|
파이썬 클래스 예제 1-7 (0) | 2022.04.23 |
파이썬 클래스 예제 1-5 (0) | 2022.04.19 |
파이썬 클래스 예제 1-4 (0) | 2022.04.18 |
파이썬 클래스 예제 1-3 (0) | 2022.04.17 |