0

This is a simple game coded in python the source code

and when run it in ubuntu 14.04

I've got this error

    File "pong.py", line 18
self.canvas = canvas
   ^
IndentationError: expected an indented block

how can I solve it thanks

1
  • I don´ t know where you got it, but there is a lot more wrong with it than just an indentation error. It lacks almost all indentation, but looking through the code, fixing (all) indentation won't fix it. Apart from that, it is written quite inefficiently for the section that I read through, even if it would work. Jun 8, 2015 at 12:44

2 Answers 2

1

In Python, the statements have to be indented by 4 spaces like this

def functionName(): //some stuff here //other stuff here

Looking at the error, this might be the problem.

3
  • 4 spaces is conventional, but as long as it is consequent, it works with any number of spaces. Jun 8, 2015 at 12:21
  • But it says here : python.org/dev/peps/pep-0008/#indentation
    – ayuhsya
    Jun 8, 2015 at 12:30
  • 1
    That is a Style Guide for creating nicely readable code. Not that it doesn't work if you style it differently (respecting the indentation levels of course. I never ran into code with another indentation then 4 spaces, but it does work. Jun 8, 2015 at 12:36
0

This question should be in Stackoverflow not in AskUbuntu. Please migrate this question there.

Apart from the above, it is a simple indentation error. You (or the coder) forgot to indent the lines in the function properly.

It should be like this:

Example: Lines 17-28 def init(self,canvas,color,paddle,paddle1): self.canvas = canvas self.paddle = paddle self.paddle1 = paddle1 self.id = canvas.create_oval(10,10,25,25, fill=color) self.canvas.move(self.id, 235,200) starts = [-3,3] random.shuffle(starts) self.x = starts[0] self.y = -3 self.canvas_height = self.canvas.winfo_height() self.canvas_width = 500

Do similarly for the other functions as well.

5
  • 1
    Not only those lines, almost the complete file has wrong indentation.
    – Timo
    Jun 8, 2015 at 10:34
  • thanks adch99 i'v edit it but its not work :( Jun 8, 2015 at 10:37
  • @MohammadAbuhassan ,Did you edit the entire file? And pls mind it def __init__(self,canvas,color,paddle,paddle1): NOT def init(self,canvas,color,paddle,paddle1): the autoformatting doesn't seem to show it.
    – adch99
    Jun 8, 2015 at 10:43
  • @adch99 got this error File 'code' "pong.py", line 30 paddle_pos = self.canvas.coords(self.paddle.id) ^ IndentationError: expected an indented block Jun 8, 2015 at 10:45
  • @MohammadAbuhassan, indent every line after the line containing def keyword till you encounter a blank line. Do this for the entire document.
    – adch99
    Jun 10, 2015 at 12:46

Not the answer you're looking for? Browse other questions tagged .