Python解决缩进错误

时间:2017-03-16 09:00:48

标签: python

我写了下面的代码但是当我尝试运行它时,我得到一个文件“C:\ Users \ Moses \ Desktop \ test.py”,第4行     定金(个体经营):       ^ IndentationError:预计会出现缩进块错误。我需要帮助。

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

类SavingsAccount(BankAccount):

def __init__(self,  balance=500.0):
self.balance = balance

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

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

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

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

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

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

类CurrentAmount(BankAccount):     def init (self,balance = 0.0):     self.balance = balance

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的新手

4 个答案:

答案 0 :(得分:1)

这是正确的缩进和逻辑上正确的代码。在获得对社区的支持之前,您必须阅读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

答案 1 :(得分:0)

你的问题在于下面的函数定义;你不能只宣告你需要给他们某种身体的空函数。

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

此外,如果您直接复制粘贴代码,那么您似乎没有使用足够的空格来缩进

即。如果我复制粘贴行

def deposit(self):

它之前只有2个空格

答案 2 :(得分:0)

Python使用缩进来表示代码块。您必须在缩进级别中保持一致。选择标签或一定数量的空格,在任何地方使用

您还必须至少有一个声明需要阻止的语句。如果必须,请使用pass语句不执行任何操作。

class BankAccount(object):
    def withdraw(self):
        pass        # Add this

    def deposit(self):
        pass        # Add this

    def other_function(self):
        if 42 != 42:
            raise EndOfTheWorldError("what")

答案 3 :(得分:0)

需要进行许多更改。

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

    退货(自我):     通

  2. 在每个if条件之后,应该有冒号:

    如果self.balance&lt; 500: