• Quiz 6a Question 7————An Introduction to Interactive Programming in Python


    

    First, complete the following class definition:

    class BankAccount:
        def __init__(self, initial_balance):
            """Creates an account with the given balance."""
            …
        def deposit(self, amount):
            """Deposits the amount into the account."""
            …
        def withdraw(self, amount):
            """
            Withdraws the amount from the account.  Each withdrawal resulting in a
            negative balance also deducts a penalty fee of 5 dollars from the balance.
            """
            …
        def get_balance(self):
            """Returns the current balance in the account."""
            …
        def get_fees(self):
            """Returns the total fees ever deducted from the account."""
            …
    

    The deposit and withdraw methods each change the account balance. The withdraw method also deducts a fee of 5 dollars from the balance if the withdrawal (before any fees) results in a negative balance. Since we also have the method get_fees, you will need to have a variable to keep track of the fees paid.

    Here's one possible test of the class. It should print the values 10 and 5, respectively, since the withdrawal incurs a fee of 5 dollars.

    my_account = BankAccount(10)
    my_account.withdraw(15)
    my_account.deposit(20)
    print my_account.get_balance(), my_account.get_fees()
    这里用了一个列表来储存数据。其他没什么特别的。
    class BankAccount:
        def __init__(self, initial_balance):
            self.balance = initial_balance
            self.fee = [0]
            
            """Creates an account with the given balance."""
           
        def deposit(self, amount):
            """Deposits the amount into the account."""
            self.balance = self.balance + amount
            return self.balance
        def withdraw(self, amount):
            """
            Withdraws the amount from the account.  Each withdrawal resulting in a
            negative balance also deducts a penalty fee of 5 dollars from the balance.
            """
            self.balance = self.balance - amount
            if self.balance < 0:
                self.balance = self.balance - 5
                self.fee[0] += 1
           
        def get_balance(self):
            """Returns the current balance in the account."""
            return self.balance
        def get_fees(self):
            """Returns the total fees ever deducted from the account."""
            return self.fee[0]*5

  • 相关阅读:
    第2章 类模板:2.3 类模板的局部使用
    第2章 类模板:2.2 类模板Stack的使用
    第2章 类模板:2.1 类模板Stack的实现
    第1章 函数模板:1.6 但是,我们不应该…?
    第1章 函数模板:1.5 重载函数模板
    第1章 函数模板:1.4 默认模板参数
    第1章 函数模板:1.3 多模板参数
    第1章 函数模板:1.2 模板参数的推导
    第1章 函数模板:1.1 初识函数模板
    第31课 std::atomic原子变量
  • 原文地址:https://www.cnblogs.com/zhurun/p/4590542.html
Copyright © 2020-2023  润新知