17: Functions in python

Debug it

Reuse sections of your code
Functions: Reuse sections of your code

Below is some code that has been deliberately broken so that you can fix it. 

The code is supposed to generate 10 random nice things to say about a computer (I know, that’s incredibly geeky, sorry):

Expected program output

Challenges

  1. Fix the syntax error

    Hint: Look at the function definition. You should have def, the name of the function, brackets (containing any parameters if there are any) then a colon :

  2. Fix the logic error so the function returns a value

    Hint: A subprogram that doesn’t return a value is a procedure not a function. This code chooses a random compliment but it doesn’t return that value

  3. Fix the logic error so 10 compliments are displayed

    Hint: This code only displays 8 compliments. Change it so that the loop executes 10 times instead.

On the next page you’ll get some ideas for projects where you can use functions in your own projects

Page 1: Intro

Page 2: The theory: learn what you need to know as fast as possible.

Page 3: Try it: try out and adapt some working python code snippets.

Page 4: Debug it: Learn how to find and fix common mistakes.

Page 5: Extend it: Choose a project idea to use your newfound python skills.

Leave a Reply