添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Hi all, i'm new here.
First of all, sorry for my bad english... I'm trying to make a simple program (i'm not a programmer) where it has to choose a random string in a list. I wrote in python, and it works, but in ESP8266 with micropython, this sentence:
random.choice(list_of_strings)
doesn't work, and retry this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'choice'

Of course i import random module. ¿It's possible that in micropython doesn't exist the attribute choice ? I know it's a silly thing to make this kind of programs in ESP8266, but i'm trying to introduce my son in programming with a simple program displaying random stupid names in a little oled screen.
Thanks!
Unfortunately random.choice isn't available in MicroPython on the ESP8266 -- it's one of the things that gets removed on the smaller microcontrollers, otherwise there wouldn't be enough room (but it is enabled on ESP32 and STM32).
On ESP8266, all you get is random.getrandbits().
But you can implement random.choice yourself -- it's the same as
def choice(x):
return x[randrange(0, len(x)]
And randrange (to get a random number between two numbers) can be copied from https://github.com/micropython/micropyt ... /random.py
  • ↳   Announcements and News
  • ↳   Newsletter archive
  • ↳   Kickstarter logistics for the Micro Python campaign
  • The MicroPython Language
  • ↳   General Discussion and Questions
  • ↳   Programs, Libraries and Tools
  • ↳   Development of MicroPython
  • Boards Running MicroPython
  • ↳   MicroPython pyboard
  • ↳   Pyboard D-series
  • ↳   WiPy and CC3200 boards
  • ↳   ESP8266 boards
  • ↳   ESP32 boards
  • ↳   micro:bit boards
  • ↳   Raspberry Pi microcontroller boards
  • ↳   Other Boards
  • Hardware Projects and Component Drivers
  • ↳   Hardware Projects
  • ↳   Drivers for External Components
  •