error: expected an indented block, python
Question:
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
Answers:
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.
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.
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
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.
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.