Day 86簡単な銀行口座クラスを作る

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

Pythonコード

1class BankAccount:2    def __init__(self, name, balance=0):3        self.name = name4        self.balance = balance5    def deposit(self, amount):6        self.balance += amount7    def withdraw(self, amount):8        if amount > self.balance:9            print('残高不足です')10        else:11            self.balance -= amount12    def check_balance(self):13        return self.balance14 15test_account = BankAccount('テスト口座', 1000)16print(test_account.check_balance())17test_account.deposit(500)18print(test_account.check_balance())19test_account.withdraw(200)20print(test_account.check_balance())

解説

次に試してみよう