Python Forum

Full Version: define a main()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I managed, with help from this site and the book 'Automate the Boring Stuff', to write several little routines. To my utter amazement, they all now work.

I start with an excel file that contains student names and numbers. (I have to do this every new term)
1. I resize the students' photos.
2. put the photos in the excel table
3. read in scores from tests each week (they come from a marking program), inserting new columns and add up the scores for each test.

I want to string these steps together to do everything in one program.

From what I read, I should define each separate routine as a function, then declare a main() which contains each routine.

Is that correct?

A problem for me is openpyxl: it does not load any images and when saved, the images in the original excel file are gone. Therefore, I need to do everything from scratch each time I add data to my main excel file. Not a problem if I can run all the routines in 1 main().
something like this
def foo():
    print('this is output from function foo')

def bar():
    print('this is output from function bar')

def main(): # this function can be names as you want
    foo() # call function foo
    bar() # call function bar

if __name__ == '__main__':
    main()
def foo():
    print('this is output from function foo')

def bar():
    print('this is output from function bar')

if __name__ == '__main__':
    foo()
    bar()
note the line if __name__ == '__main__':
it allows to import your module in other modules, but if executed as a python file what is in the body will be executed, e.g. call to main or call of foo and bar in the second example.