Python and indentation, having trouble getting started.

I just started learning python and I am catching up. I come from a background background.

class Alarm:

    def timer():

    def main():
        print ("Timer has Started")

    main()

      

I always get a silly error when I try to run this code:

alarm > python alarm.py 
  File "alarm.py", line 5
    def main():
      ^
IndentationError: expected an indented block

      

+2


source to share


9 replies


As others have pointed out, you have a syntax error because timer () has no body.

You don't need to use main () in python at all. Usually people use it to indicate that a file is a top-level program and not a module to be imported, but this is just by convention

You can also see this idiom

def main():
    blah blah

if __name__ == "__main__":
    main()

      



Here __name__

is a special variable. If the file was imported, it will contain the name of the module, so no comparison is done and main is not run.

The top-level program __name__

contains " __main__

", so the main () function will run.

This is useful because sometimes your module can run tests when loaded as a program, but you do not want this test to run if you are importing it into a larger program.

0


source


You have an empty def

def timer():

      

using



def timer():
    pass

      

instead.

+11


source


Learn about pass

statement
, main

usually not in class.

A global (level module) main()

function is simpler than a class method Alarm.main()

. Functions usually main()

come at the module level.

class Alarm:

    def timer():
        pass

def main():
    print ("Timer has Started")

main()

      

+3


source


try deindent main () and add a pass to the timer and define an init method:

class Alarm():

    def __init__(self):
        print ("Timer has Started")

<shell>$  Alarm()

      

+1


source


The timer function is not defined. (And your space / tab imprint may be mixed)

Read more about classes in the tutorial ( classes ).

class Alarm:

    def timer(self):
        pass
    def main(self):
        print ("Timer has Started")

if __name__ == '__main__':
    class_inst = Alarm()
    class_inst.main()

      

If you are into python read PEP8 .
Also, using pylint , it will indicate indentation and many other errors that you will run into before executing your code.

+1


source


I think you want to use __init__

although this is a constructor ...

class Alarm:

    def timer(self): 
        print('timer has started')

    def __init__(self): 
        print('constructor')
        self.timer()


x = Alarm()

      

Constructor

timer started

My example differs from the others in that I am actually instantiating a new object.

Notes:

  • specify self

    as first argument for any method defined in the class
  • __init__

    is the definition method for the constructor
  • call the class by doing variableName = className () as if you were calling a function, no new

    keyword
  • If you have an empty function use a keyword pass

    likedef foo(self): pass

+1


source


Calling main () will result in an undefined function error, since it is the Alarm method.

IMHO the correct form you should use is this:

class Alarm:
    def timer():
      pass

    @staticmethod
    def main():
      print ("Timer has Started")

if __name__ == "__main__" :
    Alarm.main()

      

+1


source


In Python, you don't need to define everything as a class. There is no encapsulation in this code, so there is no reason to define an Alarm class. Just execute the functions in the module.

0


source


Thanks everyone for the help. I made a little alarm / timer to remind me to get up and walk from time to time. I got most of the work and it works great. Tested it with a stopwatch and it works great.

import time

def timer(num):
    seconds = num*60
    print (num , "minutes", seconds , "seconds")

    while (seconds > 0):
        print (seconds, "seconds")
        time.sleep(1)
        seconds = seconds-1

    print ("Time to get up and take a WALK!!!!")
    main()


def main():
    number = input("Input time : ")
    int(number)
    timer(number)


if __name__ == '__main__':
    main()

      

0


source







All Articles