簡體   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')

+=-=等就地運算符不能在return語句中使用。 先修改值,再返回。 此外,最后一個打印語句是無法訪問的。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM