error: expected an indented block, python

huangapple go评论64阅读模式
英文:

error: expected an indented block, python

问题

抱歉,我不能提供代码的翻译,但我可以帮助您理解和解决问题。您提到的错误信息是"expected an indented block",这通常意味着在函数定义之后需要一个缩进块。请检查您的代码,确保在函数def __add__(self,other):之后有正确的缩进块。示例代码应该如下所示:

def __add__(self, other):
    # 在这里添加您的代码逻辑
    pass  # 或者您的代码块

确保在def __add__(self, other):下方添加您的代码逻辑,并确保使用正确的缩进。如果您仍然遇到问题,请提供更多代码上下文,以便我能够提供更具体的帮助。

英文:

so, im following a course called Python Game Development™ : Build 11 Total Games.
and I came across an error saying expected an indented block and I don't know how to fix it as the person who wrote the code has the same exact code as me. so, I don't get whats wrong because they had no error, and I did.
this is my code:

import collections

import math

import os

def path(filename):

    filepath = os.path.realpath(__file__)

    dirpath = os.path.dirname(filepath)

    fullpath = os.path.join(dirpath,filename)

    return fullpath

def line(a,b,x,y):

    import turtle

    turtle.up()

    turtle.goto(a,b)

    turtle.down()

    turtle.goto(x,y)

class vector(collections.Sequence):

    PRECISION = 6

    __slots__ = ('_x','_y','_hash')

    def __init__(self,x,y):

        self._hash = None

        self._x = round(x,self.PRECISION)

        self._y = round(y,self.PRECISION)

    @property

    #getter

    def x(self):

        return self._x

    @x.setter

    def x(self,value):

        if self._hash is not None:

            raise ValueError('Cannot set x after hashing')

        self._x = round(value,self.PRECISION)

    @property

    def y(self):

        return self._y

    @y.setter

    def y(self,value):

        if self._hash is not None:

            raise ValueError('Cannot set y after hashing')

        self._y = round(value,self.PRECISION)

    def __hash__(self):

        

        if self._hash is None:

            pair = (self.x,self.y)

            self._hash = hash(pair)

        return self._hash

    def __len__(self):

        return 2

    def __getitem__(self,index):

        if index == 0:

            return self.x

        elif index == 1:

            return self.y

        else:

            raise IndexError

    def copy(self):

        type_self = type(self)

        return type_self(self.x,self.y)

    def __eq__(self,other):

        if isinstance(other,vector):

            return self.x == other.x and self.y == other.y

        return NotImplemented

    def __ne__(self,other):

        if isinstance(other,vector):

            return self.x != other.x and self.y != other.y

        return NotImplemented

    def __iadd__(self,other):

    def __add__(self,other):

the error is on def add(self,other):. I'm a beginner programmer. so if u could help me out with this error, it would be great. thank you

答案1

得分: 2

当您复制了代码时,您忘记复制__iadd____add__中的业务逻辑,如果添加它们,错误将消失。

def __iadd__(self, other):
    # 在这里添加您的代码

def __add__(self, other):
    # 在这里添加您的代码

注意:请将# 在这里添加您的代码替换为实际的业务逻辑。

英文:

When you copied the code, you forgot to copy the the business logic in iadd and add, if you add that the error will disappear.

def __iadd__(self,other):
   # Your code goes here

def __add__(self,other):
   # Your code goes here

Note: Replace # Your code goes here with the actual business logic.

答案2

得分: 0

存在**iadd(self,other)add(self,other)**方法中的语法错误,因为没有提供实现并且方法没有正确关闭。

例如:

示例代码 1

def fun():
print("Hello")

示例代码 1的输出:

文件 "file1.py",第 2行
print("Hello")
^
缩进错误:预期缩进块

示例代码 2

def fun():
pass
print("Hello")

示例代码 2的输出:

Hello

因此,我们已经为这些方法提供了实现,或者如果不需要的话,您可以将这些方法删除。

英文:

There is a syntax error in iadd(self,other) and add(self,other) methods, because there is no implementation provided and methods are not properly closed.

For Example:

Sample code 1

def fun():
print("Hello")

Output of Sample code 1

File "file1.py", line 2
print("Hello")
    ^
IndentationError: expected an indented block

Sample code 2

def fun():
  pass
print("Hello")

Output of Sample code 2

Hello

Hence, we have provided an implementation for the method or if not needed you can remove these methods.

huangapple
  • 本文由 发表于 2023年2月8日 15:01:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定