Day 85メソッドで状態を更新する

2026-05-20 JST ・ 難易度: 中級 ・ カテゴリ: クラス

Pythonコード

1class BankAccount:2    def __init__(self, balance=0):3        self.balance = balance4    def deposit(self, amount):5        self.balance += amount6    def withdraw(self, amount):7        if amount > self.balance:8            print('残高不足です')9        else:10            self.balance -= amount11    def check_balance(self):12        return self.balance13 14test_account = BankAccount(1000)15print(test_account.check_balance())16test_account.deposit(500)17print(test_account.check_balance())18test_account.withdraw(200)19print(test_account.check_balance())

解説

次に試してみよう