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
source to share
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.
source to share
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()
source to share
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.
source to share
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
source to share
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()
source to share