title | layout | meta-description | simple-description | share | comments | strapline | aboutbox | cats | date | date-updated |
---|---|---|---|---|---|---|---|---|---|---|
Make an 8-Ball for your micro:bit |
text-width-sidebar |
Python code to turn your micro:bit into an 8-ball. |
8-Ball |
true |
true |
`random` to pick an item from a list |
Make a funky 8-ball for the microbit. It shows a different message each time it's shaken. |
easy |
2016-12-23 10:20:00 UTC |
2016-12-23 10:20:00 UTC |
{% highlight python %} {% include_relative code/8-ball.py %} {% endhighlight %}
{: .ui .dividing .header}
Each of the possible messages is held in a list. A list holds multiple values. In this example, the list has 9 values and each one of those 9 values is a string (of characters).
{% highlight python %} answers = [ "It is certain", "It is decidedly so", "Cannot predict now", "Concentrate and ask again", "Don't count on it" "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful", {% endhighlight %}
The square brackets and each item is separated by a comma. The list can also be arranged on a single line:
{% highlight python %}
answers = ["It is certain", "Dont count on it", "Ask again",]
{% endhighlight %}
{% highlight python %} if accelerometer.was_gesture("shake") is True: {% endhighlight %}
.was_gesture(gesture)
returns a True or False depending on whether gesture
was the most recently detected gesture. .was_gesture(freefall)
would return True
if the microbit was thrown into the air.
The API for the accelerometer
module lists other gestures that can be used.
{% highlight python %} display.scroll(answers.choice) {% endhighlight %}
.choice
picks a random item from the answers
list.
Using random
requires the random
module to be imported. This is done at the beginning of the program:
{% highlight python %} from microbit import * import random {% endhighlight %}
A module (like random
) is a collection of pre-existing code you can reuse.