【问题标题】:Python solving an indentation errorPython解决缩进错误
【发布时间】:2017-08-07 08:25:53
【问题描述】:

我已经编写了以下代码,但是当我尝试运行它时,我得到一个 文件“C:\Users\Moses\Desktop\test.py”,第 4 行 def 存款(自己): ^ 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 = 平衡

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

【问题讨论】:

  • raise RuntimeError('Invalid withdraw amount. 你缺少右括号
  • 所有 if 语句后加一个冒号
  • 这里有很多错误:如果不缩进并编写一些代码(即使只是pass),您就无法执行defif 语句还需要缩进和冒号 (:)。您在Raise 上也有不止一个地方缺少引号和括号。在raiseing 之后returning 毫无意义,因为你永远不会到达那里

标签: python


【解决方案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

【讨论】:

    【解决方案2】:

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

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

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

    即如果我复制粘贴该行

    def deposit(self):

    前面只有两个空格

    【讨论】:

      【解决方案3】:

      需要进行许多更改。

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

        def 撤回(自我): 通过

      2. 每个 if 条件后面都应该有冒号:

        如果 self.balance

      【讨论】:

        【解决方案4】:

        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")
        

        【讨论】:

        • IndentationError: unindent does not match any external indentation level...你的意思是它们都应该有相同的间距
        • 看看我提供的代码。您需要缩进类、函数或控制流语句的主体(例如if)。在您的一些 if 语句之后,您还缺少冒号。
        • 我建议你从一个非常简单的“hello world”程序开始,随着你越来越熟悉添加代码结构。编写一大堆不起作用的损坏代码,然后请求互联网为您修复它是学习新语言的一种糟糕方式。
        猜你喜欢
        • 1970-01-01
        • 2018-06-08
        • 1970-01-01
        • 1970-01-01
        • 2014-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多