【问题标题】:Deposit and Withdraw on Account账户存款和取款
【发布时间】:2019-11-03 20:25:43
【问题描述】:

我的代码通过在菜单上输入数字 (5) 或数字 (6) 来询问用户是否要从已输入的余额中存入或提取金额。我以为我的代码可以工作,但是当我存入或取款并在菜单上输入 (1) 以检查我的余额时,它给了我原始余额,而不是提取或存款的金额。我还认为我的每月利息和利率会起作用,但事实并非如此。我已经编写了计算来获得月利率,但是当我想显示它们时它似乎也不起作用。

主模块:

from Account import Account

id = float(input("Enter User ID: "))
annual_interest_rate = float(input("Enter Interest Rate: "))
balance = float(input("Enter balance: "))

def apply_actions(action, account):
    if action == 0:      # display ID
        print(f"Your id is {id}")
    elif action == 1:    # display balance
        print(f"Your balance is {balance}")
    elif action == 2:     # display annual interest rate
        print(f"Your Annual Interest Rate is {annual_interest_rate}")
    elif action == 3:     # display monthly interest rate
        print(f"Your monthly interest rate is {Account.monthly_interest_rate}")
    elif action == 4:     # display monthly interest
        print(f"Your Monthly Interest is {Account.get_monthly_interest}")
    elif action == 5:     # ask for money to withdraw
        to_withdraw = float(input("How much money do you want to Withdraw?"))
        account.withdraw(to_withdraw)
    elif action == 6:     # ask for money to deposit
        amount = float(input("How much money do you want to deposit?"))
        account.deposit(amount)
    elif action == 7:     # ask to exit
        exit(7)
    else:
        print("Bad index")

if __name__ == '__main__':
    acc = Account(id, balance, annual_interest_rate)

    actions = ["Display ID",
               "Display Balance",
               "Display Annual Interest Rate",
               "Display Monthly Interest Rate",
               "Display Monthly Interest",
               "Withdraw Money",
               "Deposit Money",
               "Exit"]
    while True:
        choice = int(input("Choose index in " + str(list(enumerate(actions)))))
        apply_actions(choice, acc)

帐户.py

class Account:

    def __init__(self, id, balance, annual_interest_rate):
        self.id = id
        self.balance = balance
        self.annual_interest_rate = annual_interest_rate


    def monthly_interest_rate(self):
        return self.annual_interest_rate / 12

    def id(self):
        return self.id

    def balance(self):
        return self.balance

    def annual_interest_rate(self):
        return self.annual_interest_rate


    def get_monthly_interest(self):
        return self.balance * self.monthly_interest_rate

    def withdraw(self, amount):
        if self.balance < amount:
            raise ValueError(f"Overdraft, balance less than {amount}")

        self.balance -= amount

    def deposit(self, amount):
        self.balance +=amount

【问题讨论】:

    标签: python


    【解决方案1】:

    对此感到抱歉。我打错电话了。

    这应该可以修复弹出的错误。

    class Account:
    
        def __init__(self, id, balance, annual_interest_rate):
            self.id = id
            self.balance = balance
            self.annual_interest_rate = annual_interest_rate
    
    
        def monthly_interest_rate(self):
            return self.annual_interest_rate / 12
    
        def id(self):
            return self.id
    
        def balance(self):
            return self.balance
    
        def annual_interest_rate(self):
            return self.annual_interest_rate
    
    
        def get_monthly_interest(self):
            # NOTE: You need the () after self.balance to tell Python to use the method and not the variable, or after self.monthtly_interest_rate. Otherwise, Python takes this as a function instead of a value.
            return self.balance() * self.monthly_interest_rate
    
        def withdraw(self, amount):
            if self.balance < amount:
                raise ValueError(f"Overdraft, balance less than {amount}")
    
            self.balance -= amount
    
        def deposit(self, amount):
            self.balance +=amount
    
    id = float(input("Enter User ID: "))
    annual_interest_rate = float(input("Enter Interest Rate: "))
    balance = float(input("Enter balance: "))
    
    def apply_actions(action, account):
        if action == 0:      # display ID
            print(f"Your id is {account.id}")
        elif action == 1:    # display balance
            print(f"Your balance is {account.balance}")
        elif action == 2:     # display annual interest rate
            print(f"Your Annual Interest Rate is {account.annual_interest_rate}")
        elif action == 3:     # display monthly interest rate
            print(f"Your monthly interest rate is {account.monthly_interest_rate()}")
        elif action == 4:     # display monthly interest
            print(f"Your Monthly Interest is {account.get_monthly_interest()}")
        elif action == 5:     # ask for money to withdraw
            to_withdraw = float(input("How much money do you want to Withdraw?"))
            account.withdraw(to_withdraw)
        elif action == 6:     # ask for money to deposit
            amount = float(input("How much money do you want to deposit?"))
            account.deposit(amount)
        elif action == 7:     # ask to exit
            exit(7)
        else:
            print("Bad index")
    
    if __name__ == '__main__':
        acc = Account(id, balance, annual_interest_rate)
    
        actions = ["Display ID",
                   "Display Balance",
                   "Display Annual Interest Rate",
                   "Display Monthly Interest Rate",
                   "Display Monthly Interest",
                   "Withdraw Money",
                   "Deposit Money",
                   "Exit"]
        while True:
            choice = int(input("Choose index in " + str(list(enumerate(actions)))))
            apply_actions(choice, acc)
    

    【讨论】:

    • 是对的,您有两个“平衡”变量,一个在全局范围内,一个在您的类内,当然在该类的对象内,所以您要做的是更改全局并阅读帐户内的一个
    • 您的代码给了我很多错误。我现在无法显示在程序开始时输入的余额、ID 或利率。它给了我“TypeError:'float' object is not callable”。
    • 为什么,当我调用显示 ID 时,当我在 Account.py 模块上定义了 id 时却没有?
    • User3230:我认为id 字段和id 方法之间存在冲突?
    • 是的,@phalanx 是对的:问题是你在Account 中有同名的变量和方法,如果你在调用account.id 时,Python 不知道'指的是变量id 或方法id。如果你写account.id(),那么括号里就说明调用的是方法。
    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2014-04-05
    • 2023-03-27
    相关资源
    最近更新 更多