6 Replies - 3334 Views - Last Post: 17 July 2013 - 05:41 PM Rate Topic: -----

#1 xxHAMSTERNSxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-July 13

Python error IndentationError: expected an indented block

Posted 16 July 2013 - 11:45 PM

I tried to make a program that can count from a number you type in to another number you type in!
Here's an example at what should happend!
test.count(1,10)
1
2
3
4
5
6
7
8
9
10
And i tried to do so if it is like this. It will do this!
test.count(10,1)
10
9
8
7
6
5
4
3
2
1

Here's the code on the program!
class test:
def count(self,x,y):
self.x = x
self.y = y
if x < y:
while x <= y:
print x
x += 1
else:
while x >= y:
print x
x -= 1


But when i'm running the program it says
Indentationerror: expected an indented block
PS I'm beginner in programming :stupid:/>

Is This A Good Question/Topic? 0
  • +

Replies To: Python error IndentationError: expected an indented block

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Python error IndentationError: expected an indented block

Posted 17 July 2013 - 02:05 AM

Well...

One of the very first things you should have learned is that whitespace (indentation) is critical in python. You are going to need to review from the very beginning how to write if,while, and for statements.

Also there is really no reason to try to use a class here. In fact there isn't even a reason to really write a function. You could just use range/xrange

-Mek
Was This Post Helpful? 1
  • +
  • -

#3 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 246
  • Joined: 11-May 13

Re: Python error IndentationError: expected an indented block

Posted 17 July 2013 - 08:30 AM

You basically need an indention, after ever colon ( : ). Yes Mek is right, it would be easier just to use range/xrange. Although this class, would be useful, if you were going to use this function multiple times in the program.

Here is what it should look like.
class test:
    def __init__(self, x, y): #You need this to Initiate your class.
        self.x = x
        self.y = y
        if x < y:
            while x <= y:
                print x
                x += 1
        else:
            while x >= y:
                print x
                x -= 1


You also forgot your __init__ function, which is required in all classes. To indent, just space bar four time, or tab if your application, has this feature.

This post has been edited by DblAAssassin: 17 July 2013 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2086
  • View blog
  • Posts: 3,173
  • Joined: 21-June 11

Re: Python error IndentationError: expected an indented block

Posted 17 July 2013 - 08:55 AM

View PostDblAAssassin, on 17 July 2013 - 05:30 PM, said:

Although this class, would be useful, if you were going to use this function multiple times in the program.


In that case, it could make sense to put the code into a function, but a class still doesn't make sense. A class might make sense if you want to use the code multiple times with the same x and y - then you could design the class such that x and y would be parameters to __init__ and you could call my_object.count() multiple times to use the same x and y.

Quote

class test:
    def __init__(self, x, y): #You need this to Initiate your class.
        self.x = x
        self.y = y
        if x < y:
            while x <= y:
                print x
                x += 1
        else:
            while x >= y:
                print x
                x -= 1



Your __init__ function is doing too much. An __init__ should do the bare minimum required to produce a usable instance of your class - it should generally not calculate anything and it certainly not print anything to the screen. If your class is designed such that the most common use case for it is to create an instance of it without doing anything with the instance (because everything important is happening in the constructor), you're doing it wrong.

Quote

[__init__] is required in all classes.


No, it's not.

This post has been edited by sepp2k: 17 July 2013 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#5 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 246
  • Joined: 11-May 13

Re: Python error IndentationError: expected an indented block

Posted 17 July 2013 - 09:29 AM

Classes, aren't my strong suit. Well here is what the class should look like if your consistent on using a class. I thought my __init__ was doing to much, thanks for pointing it out sepp2k.

class test:
    def __init__(self, x, y): #You need this to Initiate your class.
        self.x = x
        self.y = y

    def calculation(self,x,y):        
        if x < y:
            while x <= y:
                print x
                x += 1
        else:
            while x >= y:
                print x
                x -= 1



Could you, explain a time where the __init__ would not be used, and how do you put the dashed border around something?

This post has been edited by DblAAssassin: 17 July 2013 - 09:34 AM

Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2086
  • View blog
  • Posts: 3,173
  • Joined: 21-June 11

Re: Python error IndentationError: expected an indented block

Posted 17 July 2013 - 09:50 AM

View PostDblAAssassin, on 17 July 2013 - 06:29 PM, said:

Well here is what the class should look like if your consistent on using a class. [snip]


Your calculation method never uses self.x or self.y, so the class becomes pointless and calculation might just as well be a stand-alone function. If you want to use self.x and self.y, the class should look like this instead:

class Counter:
    def __init__(self, x, y): #You need this to Initiate your class.
        self.x = x
        self.y = y

    def count(self):
        x = self.x
        y = self.y

        if x < y:
            while x <= y:
                print x
                x += 1
        else:
            while x >= y:
                print x
                x -= 1

# Usage:

counter = Counter(23, 42)
counter.count()
counter.count() # Again!



Quote

Could you, explain a time where the __init__ would not be used


Mostly I just meant that you can write a class without __init__, not that there's necessarily any situation where that makes sense. I can't really think of a real-world scenario right now, where there's nothing for __init__ to do (at least not one where using a class would actually be appropriate).

Quote

and how do you put the dashed border around something?


Write "[ il]inline code[ /il]", but without the spaces
Was This Post Helpful? 2
  • +
  • -

#7 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Python error IndentationError: expected an indented block

Posted 17 July 2013 - 05:41 PM

sepp2k said:

I can't really think of a real-world scenario right now, where there's nothing for __init__ to do (at least not one where using a class would actually be appropriate)

Mixins are a general exception to this I would say. Sometimes you have a collection of methods that you want other classes to be able to inherit and use (or possibly your class is just getting so long that you want to be able to separate out some of the methods into an inheritable, logically concise unit). In this case an __init__ is unnecessary (some of the time).

-Mek
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1