samedi 15 octobre 2022

micro:bit python - random library only generates one number

I am making a hangman game on my microbit and am having trouble with the random library. I need to choose a random number between 1 and 7, and every single time I run the program it always yields the number '5'.The strangest part is when I copy the exact same code into a IDE like visual code studio and run in via the terminal instead of through the micro:bit emulator it creates, like I would expect, random numbers between 1 and 7

What I have tried

Attempt 1

from microbit import * #omitted when ran in terminal
import random

num = random.randint(1, 7)
display.show(str(num)) #changed to print(num) when ran in terminal

Attempt 2

from microbit import * #omitted when ran in terminal
import random

numbers = [1, 2, 3, 4, 5, 6, 7]
num = random.choice(numbers)
display.show(str(num)) #changed to print(num) when ran in terminal

I have been following the documentation on the random library on micropython's offical docs. Is there something I am doing wrong?




Aucun commentaire:

Enregistrer un commentaire