3c. Prof. Rosenthal’s Review#

A review of Chapter 3: Functions, for students who have already studied the chapter. Use the two lists under Concepts covered as a checklist (click a list’s title to hide it), then work through the Questions, which go from easy to hard. Each question has a suggested answer under Answer. Try the question first, then open the Answer.

Run this page on Colab

Concepts covered#

Questions#

The questions use functions, print, strings and for loops.

Question 1 (easy): what is displayed?#

Predict what this code displays, then open the Answer to check.

def greet():
    print('Hello')
    print('Goodbye')

greet()
greet()

Question 2 (easy): what is displayed?#

def first():
    print('one')

def second():
    print('two')

second()
print('three')
first()

Question 3 (easy): what is displayed?#

def repeat(word, n):
    print(word * n)

repeat('ab', 3)
repeat('-', 10)

Question 4 (easy): what is displayed?#

for i in range(4):
    print(i, i * i)
print('done')

Question 5 (easy): write a function#

Write a function called underline that takes a string text and displays it, with a line of - characters below it, as long as the text.

Start from this header (replace ... with the body):

def underline(text):
    ...

Examples

underline('Hello')
Hello
-----
underline('Python 3')
Python 3
--------

Question 6 (medium): what is displayed?#

def start():
    print('start begins')
    middle()
    print('start ends')

def middle():
    print('in middle')

start()

Question 7 (medium): what is displayed?#

def show(x):
    x = x + 1
    print('inside:', x)

x = 10
show(x)
print('outside:', x)

Question 8 (medium): what is displayed?#

def stars(n):
    print('*' * n)

for i in range(4):
    stars(i + 1)
print()
stars(2)

Question 9 (medium): what is displayed?#

What does this code display when it is run as a notebook cell? Does it display Hello?

def greet():
    print('Hello')

greet

Question 10 (medium): local variables and wrong calls#

Run this cell to define add_and_show.

def add_and_show(a, b):
    total = a + b
    print(a, '+', b, '=', total)

Two correct ways to call it:

add_and_show(2, 3)
2 + 3 = 5
add_and_show(10, -4)
10 + -4 = 6

For each of the three parts, predict what is displayed and which error happens.

Part a

add_and_show(1, 2)
print(total)

Part b

add_and_show(5)

Part c

add_and_show(1, 2, 3)

Question 11 (medium): write a function#

Write a function called print_right that takes a string text and an integer width, and displays text with enough spaces in front of it that its last character is in column width. You can assume text has at most width characters.

Start from this header (replace ... with the body):

def print_right(text, width):
    ...

Examples

print_right('Monty', 10)
print_right('Python', 10)
print_right('Flying Circus', 20)
     Monty
    Python
       Flying Circus
print_right('abc', 3)
print_right('abc', 4)
abc
 abc

Question 12 (medium): refactor into a function#

This code displays two verses of a song. The verses are the same except for the animal and its sound.

print('Old MacDonald had a farm')
print('And on that farm he had a cow')
print('With a moo moo here')
print('Old MacDonald had a farm')
print('And on that farm he had a duck')
print('With a quack quack here')

Write a function verse that takes animal and sound and displays one verse, so that the code above can be replaced by two calls: verse('cow', 'moo') and verse('duck', 'quack').

Start from this header (replace ... with the body):

def verse(animal, sound):
    ...

Examples

verse('cow', 'moo')
Old MacDonald had a farm
And on that farm he had a cow
With a moo moo here
verse('pig', 'oink')
Old MacDonald had a farm
And on that farm he had a pig
With a oink oink here

Question 13 (hard): what is displayed?#

def line(n):
    print('-' * n)

def block(n, times):
    for i in range(times):
        line(n)
    print('end of block')

def page():
    block(3, 2)
    block(5, 1)

page()

Question 14 (hard): tracebacks#

Run this cell to define inner and outer.

def inner(value):
    print(value + 1)

def outer(value):
    print('outer got', value)
    inner(value)

Two correct ways to call it:

outer(5)
outer got 5
6
outer(2.5)
outer got 2.5
3.5

Predict what this call displays and which error happens. In the traceback, which function is listed first, and which one last?

outer('hi')

Question 15 (hard): write a function#

Write a function called pyramid that takes a string char (one character) and an integer height, and displays a centered pyramid with height lines. Line 1 has 1 copy of char, line 2 has 3, line 3 has 5, and so on; each line has enough spaces in front of it to be centered above the last line.

Start from this header (replace ... with the body):

def pyramid(char, height):
    ...

Examples

pyramid('*', 3)
  *
 ***
*****
pyramid('#', 5)
    #
   ###
  #####
 #######
#########

Question 16 (hard): write two functions#

Write two functions that display a grid of n by n cells:

  • print_border(n) displays one border line: n copies of +---, then a final +.

  • print_grid(n) displays the whole grid. It uses print_border, and between two border lines it displays a line of n copies of |   (a bar and three spaces), then a final |.

Start from this header (replace ... with the body):

def print_border(n):
    ...

def print_grid(n):
    ...

Examples

print_border(3)
+---+---+---+
print_grid(2)
+---+---+
|   |   |
+---+---+
|   |   |
+---+---+
print_grid(3)
+---+---+---+
|   |   |   |
+---+---+---+
|   |   |   |
+---+---+---+
|   |   |   |
+---+---+---+

Summary and questions by Prof. Y. Rosenthal, based on Think Python: 3rd Edition by Allen B. Downey. Text license: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International