2017-03-16 96 views
-7

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

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 初始化(个体,平衡= 0.0): self.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

+0

'raise RuntimeError('Invalid withdraw amount。'你错过了一个右括号 – Pearley

+0

和冒号后面的所有if语句 – languitar

+1

这里有很多错误:如果没有缩进然后编写一些代码(即使它只是'传递'),你也不能'def'。 if语句还需要缩进和冒号(':')。你也在超过1个地方失去了你的'提升'的引用和偏执。 ''raise''后'返回''没有意义,因为你永远不会到达 –

回答

0

你的问题在于下面的函数定义;你不能声明你需要给它们某种形式的空函数。

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

此外,如果有直接复制粘贴你的代码,它看起来像你没有使用足够的空间来缩进

也就是说,如果我复制粘贴线

def deposit(self):

它只有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") 
+0

IndentationError:unindent不匹配任何外部缩进级别......你的意思是他们应该都有相同的间距 –

+0

看看我提供的代码。您需要缩进诸如类,函数或控制流语句(如'if')的主体。在你的一些if语句之后,你也会错过冒号。 –

+0

我建议你从一个非常简单的“你好世界”程序开始,并添加代码结构,因为你更熟悉。编写一个不起作用的破解代码,然后要求互联网为你解决这个问题是学习一种新语言的一种可怕的方式。 –

0

需要进行许多更改。

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

    高清撤销(个体经营): 通

  2. 每个如果条件之后,应该有冒号:

    if self.balance < 500:

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