Functions & Global Variables - Rock Paper Scissors
Python ·Project: Rock, Paper, Scissors! 🎮
We’re going to make our own version of rock, paper, and scissors with the micro:bits!
Game Plan
To do that, we’ll need to:
- Generate a random number from 1 to 3
- If random number is 1, show rock. If it’s 2, show paper. If it’s 3, show scissors.
- Repeat steps 1 and 2 every time we shake the micro:bit.
Step 1: Generate a Random Number
Generate a random number from 1 to 3. Any guesses on how to do this?
Hint: Use randint.
Answer
```python hand = randint(1, 3) ```Step 2: Show the Correct Icon
If random number is 1, show rock. If it’s 2, show paper. If it’s 3, show scissors.
To show the symbols, use show_icon with the symbols below:
- Rock:
basic.show_icon(IconNames.SMALL_SQUARE) - Paper:
basic.show_icon(IconNames.SQUARE) - Scissors:
basic.show_icon(IconNames.SCISSORS)
Try this one yourself!
Solution
```python hand = randint(1, 3) if hand == 1: basic.show_icon(IconNames.SMALL_SQUARE) elif hand == 2: basic.show_icon(IconNames.SQUARE) else: basic.show_icon(IconNames.SCISSORS) ```Step 3: Shake to Play
Repeat steps 1 and 2 every time we shake the micro:bit.
Hint: input.on_gesture(Gesture.SHAKE, on_gesture_shake) makes it so that every time the micro:bit shakes, it runs the function on_gesture_shake.
Put steps 1 and 2 in a function called on_gesture_shake.
def on_gesture_shake():
# Write the code for steps 1 and 2 here
hand = randint(1, 3)
if hand == 1:
basic.show_icon(IconNames.SMALL_SQUARE)
elif hand == 2:
basic.show_icon(IconNames.SQUARE)
else:
basic.show_icon(IconNames.SCISSORS)
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
Complete Code
def on_gesture_shake():
hand = randint(1, 3)
if hand == 1:
basic.show_icon(IconNames.SMALL_SQUARE)
elif hand == 2:
basic.show_icon(IconNames.SQUARE)
else:
basic.show_icon(IconNames.SCISSORS)
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
Time to Play!
Download the code onto your micro:bit and partner up! Shake to play Rock, Paper, Scissors!
Global Variables
Last week you saw this:
def on_button_a():
global rope
rope += -0.1
As you can see we used the keyword global here. What does that mean?
The Problem: Local Variables
Let’s say we want to show the number of times we press Button A.
def on_button_pressed_a():
count = 0
count += 1
basic.show_number(count)
input.on_button_pressed(Button.A, on_button_pressed_a)
Try running this. What happens?
The Problem: count here is a local variable. That means it only exists inside the function. Every time we call the function, count resets to 0!
The Solution: Global Variables
If we move count to outside of the function, it’s now a global variable. If we want to use a global variable inside a function, we must type global before the variable name.
count = 0
def on_button_pressed_a():
global count
count += 1
basic.show_number(count)
input.on_button_pressed(Button.A, on_button_pressed_a)
Now try it! What happens?
The Result: The count increases each time you press the button because the variable persists between function calls!
Key Points About Global Variables
- Global variables are defined outside of functions
- They can be accessed from anywhere in your code
- To modify a global variable inside a function, you must use the
globalkeyword - To read a global variable, you don’t need
global, but it’s good practice to use it
Challenge: Show Number of Games Played
How can we keep track of the number of games played in Rock, Paper, Scissors without it resetting every time the shake function is called?
Hint: Use a global variable!
Solution
```python games_played = 0 def on_gesture_shake(): global games_played games_played += 1 hand = randint(1, 3) if hand == 1: basic.show_icon(IconNames.SMALL_SQUARE) elif hand == 2: basic.show_icon(IconNames.SQUARE) else: basic.show_icon(IconNames.SCISSORS) basic.pause(1000) basic.show_number(games_played) input.on_gesture(Gesture.SHAKE, on_gesture_shake) ```Step Counter Challenge
Let’s take it a step further! Make the micro:bit show how many times we’ve shaken it. One shake = one step.
steps = 0
def on_gesture_shake():
# You write the code here!
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
Solution
```python steps = 0 def on_gesture_shake(): global steps steps += 1 basic.show_number(steps) input.on_gesture(Gesture.SHAKE, on_gesture_shake) ```What We Learned Today
✓ How to use randint() to generate random numbers
✓ How to build a Rock, Paper, Scissors game
✓ The difference between local and global variables
✓ How to use the global keyword to modify variables inside functions
✓ How to track game statistics using global variables
Key Takeaways
- Local variables only exist inside the function where they’re created
- Global variables exist throughout your entire program
- Use
global variable_nameinside a function to modify a global variable - Global variables are perfect for tracking things like scores, counts, or game state!