简体   繁体   English

我正在使用 OOPS 概念使用银行 class 进行存款和取款。 但它显示语法错误

[英]I am using OOPS concept to make deposit and withdrawl using bank class. But it's showing syntax error

class Bank:
    def __init__(self,owner,balance):
        self.owner = owner
        self.balance = balance
    def __str__(self):
        return f"Bank owner:{self.owner}\n Bank balance:{self.balance}"
    def deposit(self,amount):
        self.amount = amount
        print('Deposit accepted')
        return self.balance += self.amount
    def withrawl (self,amount):
        if self.amount>self.balance:
            print('Withdrawl exceeded the available balance')
        else:
            return self.balance -= self.amount
            print('Withdrawl accepted')

In-place operators like += and -= cannot be used in a return statement. +=-=等就地运算符不能在return语句中使用。 Modify the value first, then return it.先修改值,再返回。 Also, the last print statement is unreachable.此外,最后一个打印语句是无法访问的。

class Bank:
    def __init__(self, owner, balance):
        self.owner = owner
        self.balance = balance

    def __str__(self):
        return f"Bank owner:{self.owner}\n Bank balance:{self.balance}"

    def deposit(self, amount):
        print('Deposit accepted')
        self.balance += amount
        return self.balance

    def withdrawl(self, amount):
        if amount > self.balance:
            print('Withdrawl exceeded the available balance')
        else:
            self.balance -= amount
            print('Withdrawl accepted')
            return self.balance

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在使用 tkinter 构建一个应用程序,但是显示“top”为无效语法的错误,我该如何解决这个问题 - i am building a app using tkinter but theres an error showing “top” as an invaild syntax, how do i solve this 使用 class 内部的函数时出错。 我无法将 function 分配给新的 class 方法 - Error using functions inside of a class. I can't assign a function to a new class method 我正在尝试使用 oops 开发 tkinter 应用程序但收到此错误 - I'm trying to develop tkinter app using oops but getting this error 在 python 中使用 OOPS 概念制作图书馆管理系统时发生错误 - ValueError:要解压的值太多(预期 2) - An Error occured while making a Library Management System using OOPS concept in python- ValueError: too many values to unpack (expected 2) 为什么使用 plotly 制作 plot 时,我收到此代码的无效语法错误? - Why am I receiving an invalid syntax error for this code to make a plot using plotly? 我正在创建自己的课程。 _Screen.__init__() 采用 1 个位置参数但给出了 4 个类型错误。 我究竟做错了什么? - I am creating my own class. There's a type error that _Screen.__init__() takes 1 positional argument but 4 were given. What am I doing wrong? 创建考虑存款和取款的功能 - Creating Functions that take into account Deposit and Withdrawl 我收到类中定义的属性的属性错误。 我怎样才能解决这个问题? - I am getting an attribute error for an attribute defined inside the class. How can I fix this? 我正在重写HTMLCalendar类中的方法。 但它给了我一个错误。 我该如何正确地继承这个? - I am overriding a method in HTMLCalendar class. But it gives me an error. How do I inherit this correctly? 为什么在使用 >>= 运算符时会出现语法错误? - Why am I getting a syntax error when using the >>= operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM