ctivity 1
Defining and calling functions
Load Thonny and create a new file, save it as Dragon Realm.
1. Type the following code:
2. Leave a blank line then add the following code:
3. Run the file.
Why does nothing happen?
- All the code you have written is inside a function.
- In order to get this code to run, we need to call the function...
4. In Thonny, in the interactive script window type:
displayIntro()
...then press the Enter key... AHA!
Explain...
Activity 2
Returning values from functions
Add the following code to your program:
- Run the program
- In the shell, type the following and press enter:
- chooseCave()
The program should ask you the question:
…and then output’s your answer
Explain what this function is doing.
Specifically mention:
- The input() function.
- Storing a value in a variable.
- The effect of using the return keyword.
Activity 3
Passing values to functions
Type the following text:
Run the program
- In the shell, type the following and press enter:
- checkCave()
The program will generate an error, why?
Reading the error message, on the 3rd line, the error states ‘missing 1 required positional argument’.
Arguments are values that must be inputted to some types of functions. If you look at the function definition (the 1st line), of CheckCave(), you can see that the function does indeed require an input – that is the bit between he bracket. We call these inputs to functions, parameters.
So let's try this again, but this time we’ll pass the cave variable that we captured when we ran chooseCave() in the last task:
Activity 4
Automating a program
Your final task, is to add instruction to the bottom of your program to automate the process of playing the game.
You have already run the instruction you need
Extension task
Add a while loop to enable player to play the game again once it ends.