Program structure with a main() function

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Program structure with a main() function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should you call main() in a simple Python program?
A At the very top, before any functions are defined
B At the end of the file, after the functions are defined
2. Which line correctly defines a main function?
A def main()
B def main():
3. What does main() hold in a well-structured program?
A The main steps of the program
B Only the comments for the program
4. Which code actually starts the program?
A def main(): print("Hi") main()
B def main(): print("Hi")
5. True or false: you should call main() once at the end of the file.
A True
B False

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1def main(): ACalls the main function once at the end of the file to start the program
2main() Bmain() is called before it is defined, which causes a NameError. Move main() to the end of the file, after all the functions.
3Helper functions are defined above main, so they already exist when main runs Cdef greet(): print("Hi!") def main(): greet()
4The program starts by calling main() once at the very end of the file, with no indent Ddef main(): print("Start") main()
5def main(): print("Start") Emain() is never called, so the program does nothing. Add main() at the end of the file with no indent.
6main() def greet(): print("Hi!") def main(): greet() FDefines the main function that holds the main steps of the program

Definitions

def main():

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
___ main(): print("Let's go!") main()
2.
def main(): print("Start") ____()
3.
def main()_ print("Game on") main()
4.
def main(): _____("Hello") main()
5.
def ____(): print("Welcome") main()

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def main()
B name = input('Name? ')
C print('Hi', name)
D main()
Question 2
A def main():
B Print('Start')
C print('End')
D main()
Question 3
A import random
B define main():
C n = random.randint(1, 6)
D print(n)
E main()
Question 4
A def main():
B x = 5
C print(y)
D main()
Question 5
A def hello():
B print('Hello')
C def main():
D hello
E main()

Answer Key

Program structure with a main() function
Section A: Quick Questions
Q1
B — At the end of the file, after the functions are defined
Q2
B — def main():
Q3
A — The main steps of the program
Q4
A — def main(): print("Hi") main()
Q5
A — True
Section B: Match the Pairs
Q1
def main(): → Defines the main function that holds the main steps of the program
Q2
main() → Calls the main function once at the end of the file to start the program
Q3
Helper functions are defined above main, so they already exist when main runs → def greet(): print("Hi!") def main(): greet()
Q4
The program starts by calling main() once at the very end of the file, with no indent → def main(): print("Start") main()
Q5
def main(): print("Start") → main() is never called, so the program does nothing. Add main() at the end of the file with no indent.
Q6
main() def greet(): print("Hi!") def main(): greet() → main() is called before it is defined, which causes a NameError. Move main() to the end of the file, after all the functions.
Section C: Definitions
Q1
def main(): — Defines a function called main that holds the main steps of your program. You call main() once at the end of the file, after the other functions are defined, to start the program.
Section D: Fill in the Blanks
Q1
def main(): print("Let's go!") main()
Q2
def main(): print("Start") main()
Q3
def main(): print("Game on") main()
Q4
def main(): print("Hello") main()
Q5
def main(): print("Welcome") main()
Section E: Spot the Error
Q1
A — The first line of a function definition must end with a colon. It should be def main():
Q2
B — Python is case-sensitive, so Print is not the same as print. The step inside main() should be print('Start')
Q3
B — Python uses the keyword def to create a function, not define. It should be def main():
Q4
C — The variable y was never created inside main(). The program made x, so it should be print(x)
Q5
D — Without brackets, hello only names the function and does not run it. A function call needs brackets: hello()