Create a Rock-Paper-Scissors game with Python and micro:bit [Part 2]
Tags: python, microbit, tutorial, beginners
Published: 2020-12-26 12:59:02

---
canonical_url: https://www.foteinisavvidou.codes/rock-paper-scissors-python-microbit-part2/
description: In this project you will learn basic programming concepts by creating a Rock-Paper-Scissors game using Python and micro:bit.
---

| Project goal | Get started with python and micro:bit by creating a Rock-Paper-Scissors game |
| --- | --- |
| **What you’ll learn** | Global variables and lists in Python |
| **Tools you’ll need** | A modern browser |
| **Time needed to complete** | 30 minutes |


### What You'll Build
In the second part of the project you will improve the [Rock-Paper-Scissors game](https://dev.to/sfoteini/create-a-rock-paper-scissors-game-with-python-and-micro-bit-part-1-445o), that you have already made, by creating variables for storing the current game round and players’ score. You can find the final project at [Microsoft Make Code](https://makecode.microbit.org/_JK43WRMR7Xqz) and you can download the code from my [GitHub repository](https://github.com/sfoteini/rock-paper-scissors-microbit).

### Instructions
Visit the [Microsoft Make Code](https://www.microsoft.com/en-us/makecode) website, choose the micro:bit and then select the previous Rock-Paper-Scissors project.
If you followed my instructions from the [first part](https://dev.to/sfoteini/create-a-rock-paper-scissors-game-with-python-and-micro-bit-part-1-445o) of the project, you should have a function called `on_gesture_shake`. This function contains all the game code that runs when you shake the micro:bit. You also defined two variables `hand1` and `hand1` for the first and second player respectively and you used the function `randint()` to make a random choice for each player. Finally, you showed players’ selected objects and the number of the winner by using multiple `if` statements.

### Create a variable named rounds to store the current round
Create the variable `rounds` with a value `0` outside of the function (before starting the game the number of round is 0). The variable `rounds` is a global variable.
> Variables that are created outside of all functions, are called **global** variables. In order to change a global variable inside a function, you should use the `global` keyword.

In the beginning of every round, increase the `rounds` value by 1.
```python
rounds = 0
def on_gesture_shake():
    global rounds
    rounds += 1
    ...
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
```
> The expression `rounds += 1` is equal to `rounds = rounds + 1`. This expression means that we get the current value of `rounds`, we add `1` and then we store the new value in the `rounds` variable.

### Modify the winner selection code
In the part 1, we used multiple `if` statements (nested if statements) in order to determine the winner and the function `basic.show_number()` to show the number of the player that won. In this project, you will modify this code snippet by creating a variable named `winner` in which you will store the number of the player that won (or 0 if none of the players won).
For instance, suppose that the first player wins. In this case, you use the function `basic.show_number(1)` in order to print the number 1. Replace this function with the expression `winner = 1`. Similarly, if the second player wins, use the expression `winner = 2` and if it’s a tie, use the expression `winner = 0`.
Finally, use the command `basic.show_number(winner)` to show the number that is stored in the `winner` variable.
```python
if hand1 == 1: # Rock
    if hand2 == 1: # Rock
        winner = 0
    elif hand2 == 2: # Paper
        winner = 2
    else: # Scissors
        winner = 1
elif hand1 == 2: # Paper
    if hand2 == 1: # Rock
        winner = 1
    elif hand2 == 2: # Paper
        winner = 0
    else: # Scissors
        winner = 2
else: # Scissors
    if hand2 == 1: # Rock
        winner = 2
    elif hand2 == 2: # Paper
        winner = 1
    else: # Scissors
        winner = 0
basic.show_number(winner)
```

### Create a variable named score to store the player’s score
You can create two variables, `score1` and `score2`, to store the score of the first and the second player, respectively. In this project, we are going to use a list instead.
> A list is a data structure that allows you to store various data in it. This means that you can store many elements or values in a list. In Python, lists are created by placing the items inside of a square bracket ([ ]) separated by comma (,). For example, the following command creates a list named `numbers` filled with numbers in the range of 1 to 5: `numbers = [1, 2, 3, 4, 5]`.

Create the list `score` with a value `[0, 0]` outside of the function (before starting the game the players’ score is 0). The first list item corresponds to the first player’s score and the second list item to the second player’s score. 
```python
score = [0, 0]
```

### Update the score in every round
Use `if` statements and the variable `winner` (in which you have stored the number of the player that won) in order to increase by 1 the score of the first or the second player, at the end of every round.
> How to access list item? Every list item is assigned a number in range 0 to n-1, where n is the total number of the list items. These numbers are called indexes. You access a list item by referring to its index number. For example, suppose that you create a list named `list1`. In order to access the first item, you use `list1[0]`, and for the third item you use `list1[2]`.
```python
if winner == 1:
    score[0] += 1
elif winner == 2:
    score[1] += 1
```

You will print the players score in the form of “1-3”. The first number corresponds to the first player’s score and the second one to the second player’s score. In order to print the score in that form use the following command:
```python
basic.show_string(str(score[0]) + "-" + str(score[1]))
```
The function `basic.show_string()` is used for printing characters. However, the values `score[0]` and `score[1]` are numbers, and thus you have to convert them first into characters using the `str()` function. The plus (+) symbol is used for string concatenation. For example, the following code snippet:
```python
a = "Hello, "
b = "John!"
result = a + b
print(result)
```
will print: “Hello, John!”.

### Improve user experience
Use `pause()` and `clear_screen()` functions exactly the same way you used them in the first part of the project, in order to make a smooth transition between images.

Congratulations! You develop a Rock-Paper-Scissors game with Python and micro:bit. You can download the code from my [GitHub repository](https://github.com/sfoteini/rock-paper-scissors-microbit).
If you want to share the project with your friends, you can select the Share button.

---
More articles: [foteinisavvidou.codes](https://www.foteinisavvidou.codes/)

Hello, my name is Foteini Savvidou and I am an Electrical and Computer Engineering student who loves writting code and creating beautiful things. I am interested in computer science, AI and especially in applications of technology in healthcare and education. I want to use technology to improve people's everyday life, and create a more sustainable 🌱, accessible and inclusive 🧑‍🤝‍🧑 world.

✨ [Blog](https://www.foteinisavvidou.codes/) | [LinkedIn](https://www.linkedin.com/in/foteini-savvidou) | [Twitter](https://www.twitter.com/ClairSavvidou) | [GitHub](https://github.com/sfoteini)