Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
define a main()
#1
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().
Reply
#2
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020