Let’s Create a Hi Low Guessing Game for the Microbit using Python code. The way that the program will work is that we will randomly generate an integer between 1 and 10 and ask the user to try to guess that number . The user will guess that number based on how many times they press the button_a . We will use a while True loop to keep the program running until the user guesses the randomly generated number. This is a multi-part lesson.
Part I. Warm-up .
Create a variable called cpuNumber that stores a random int between 0-9 . Below is the first part of the code. Please
1 2 3 4 |
from microbit import * import random cpuNum = |
Can you finish the line 4 up above? (Scroll down to see solution)
1 2 3 4 |
from microbit import * import random cpuNum =random.randrange(9) |
Can you finish the line 4 up above? (Scroll down to see solution)
Now, we are close. However, the python code will generate a random integer between 0 and 9.
How can we generate a random number between 1 and 10?
I’ll give you two hints.
Hint #1 : It involves a little arithmetic
Hint #2 : You want to increase the entire range 0-9 by 1 so that the new range is 1-10
Scroll down to see one of the ways to do it.
1 2 3 4 |
from microbit import * import random cpuNum = random.randrange(9) + 1 |
As you can see above, all you have to do is add 1 to the last line of code!