You have been thinking about how to break this project down into smaller tasks or functions. You should have already drawn a structure chart.
In this lab we will learn how to implement these smaller tasks into functions. Each of these functions should complete one logical task each.
Ensure that you are signed into GitHub
Accept the assignment: Project 4 - Dice Roller
Clone the repository to your computer using GitHub Desktop, or open it online using Codespaces
Open lab1.py
In Python, a function is defined using the def keyword, followed by the function name, a pair of parentheses, and a colon. The body of the function is then indented under this definition.
Notice that we can then 'call' the function by its name. In the following example, we run the greet function multiple times.
This is one of the benefits of functions, being able to re-use sections of code.
A function in programming is like a small machine in a factory: you give it raw materials (inputs), it does its job (processes the inputs), and then it hands you a finished product (the output). Sometimes, while the machine is working, it might also show a gauge reading or make a noise (a 'side effect'), like printing something to the console. However, the main purpose of a machine – or a function – is to complete its task and deliver a final product to you. In programming, we say that a function 'returns' a result. This means that after the function has finished processing the information you've given it, it gives you something back: a value that you can use elsewhere in your program.
We are used to code running from top to bottom, but now with functions things get a little more interesting:
Call Function:
- Line 5 runs first as functions only run when 'called'
- name = get_name() tells the computer to run the get_name function.
Ask for User Input:
- Once called we jump up to line 2
- input("Enter name: ") inside get_name function pauses and waits for the user to type their name and then press enter.
Function Returns Name:
- Once the user presses enter we will have a name stored in the name variable, so we move on to line 3
- return name in the get_name function sends the name back to where the function was called (line 5)
Store Returned Name:
- We are now back to line 5 as the function has finished and returned a value
- The name given back by get_name is stored in the name variable outside the function.
Print Greeting:
- print(f"Hello, {name}") displays "Hello" followed by the name the user entered.
Complete the following requirements:
Write a function named welcome_message that prints out "Welcome to our program!" whenever it is called.
Create a function named print_divider that prints out a line of asterisks (e.g., "**********") to act as a section divider.
Write a function named get_number that asks the user to input a whole number and then returns the result as an integer.
Create a function named get_random_colour that, when called, returns a random colour from a predefined list of colours.
Call all of your functions to demonstrate that they work
Press Ctrl + F5 to run the program or open a terminal and type python lab1.py
You should now know how to create and use functions to perform tasks and return results in Python.
Let's wrap up this lab by pushing our code to GitHub:
Enter a commit message. E.g. "Lab 1 complete"
Press Commit to main
Press Push origin
Here is an optional YouTube video that covers lab 1 and 2 - how to use functions in Python.
This may be useful if you need more information or have missed a lesson.