Python解缩进

2024-09-30 12:11:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了下面的代码,但是当我试图运行它时,我得到了一个文件“C:\Users\Moses\Desktop”\测试.py“,第4行 def存款(自身): ^ IndentationError:应为缩进块错误。我需要帮助。在

class BankAccount(object):
def withdraw(self):
    pass
def deposit(self):
    pass

类节省帐户(银行帐户):

^{pr2}$

类别当前金额(银行帐户): definit(自身,平衡=0.0): 自我平衡=平衡

def deposit(self,deposit_amount)
self.balance += deposit_amount
  return self.balance

  if amount < 0:
    raise RuntimeError('Invalid deposit amount.')
  return self.balance


def withdraw(self, withdraw_amount):   
   self.balance -= withdraw_amount
  return self.balance

  if withdwa_amount < 0:
    raise RuntimeError('Invalid withdraw amount')
  return self.balance

  if withdwa_amount > self.balance:
    raise RuntimeError('Cannot withdraw beyond the current account balance')
  return self.balance

我需要一些关于缩进误差的帮助,知道它是什么以及如何解决它。我是python新手


Tags: 代码selfreturnifdefpassamountraise
3条回答

需要进行许多更改。在

  1. 如果不想定义方法,可以使用pass

    def提取(自行): 通过

  2. {cd2>之后应该有一个冒号

    如果自我平衡<;500:

您的问题在于下面的函数定义;您不能只声明空函数,而需要为它们提供某种主体。在

class BankAccount(object):
    def withdraw(self):
        pass
    def deposit(self):
        pass

另外,如果您直接复制粘贴了代码,那么看起来您没有使用足够的空格来缩进

例如,如果我复制粘贴线

def deposit(self):

它前面只有两个空格

这是正确的缩进和逻辑正确的代码。在获得社区支持之前,您必须阅读python编码指南。在

class BankAccount(object):
    def withdraw(self, withdraw_amount):
        pass

    def deposit(self, deposit_amount):
        pass


class SavingsAccount(BankAccount):
    def __init__(self,  balance=500.0):
        self.balance = balance

    def deposit(self, deposit_amount):
        if deposit_amount < 0:
            raise RuntimeError('Invalid deposit amount.')
        self.balance += deposit_amount
        return self.balance

    def withdraw(self, withdraw_amount):
        if self.balance < 500:
            raise RuntimeError('Cannot withdraw beyond the minimum account balance')

        if withdraw_amount > self.balance:
            raise RuntimeError('Cannot withdraw beyond the current account balance')

        if withdraw_amount < 0:
            raise RuntimeError('Invalid withdraw amount.')

        self.balance -= withdraw_amount
        return self.balance


class CurrentAmount(BankAccount):
    def __init__(self,  balance=0.0):
        self.balance = balance

    def deposit(self,deposit_amount):
        if deposit_amount < 0:
            raise RuntimeError('Invalid deposit amount.')

        self.balance += deposit_amount
        return self.balance

    def withdraw(self, withdraw_amount):  
        if withdraw_amount < 0:
            raise RuntimeError('Invalid withdraw amount')

        if withdraw_amount > self.balance:
         raise RuntimeError('Cannot withdraw beyond the current account balance')

        self.balance -= withdraw_amount
        return self.balance

相关问题 更多 >

    热门问题