micropython password generator help

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
koalaburger
Posts: 2
Joined: Thu Apr 16, 2020 4:43 am

micropython password generator help

Post by koalaburger » Thu Apr 16, 2020 4:58 am

Hi everyone

I'm a Industrial Tech teacher at a high school in Australia - and I'm a little stuck with this code... I can't get random.shuffle to work, I'm quite certain I didn't do this quite right...

Any help would be most appreciated.

Thanks!

I've been using this as my guide - https://www.101computing.net/random-password-generator/

Ultimately what I'm trying to do is:
Input A = random 4 digit PIN
Input B = random 12 digit chars
else = HEART

---------------------------------

Code: Select all

from microbit import *
import random

def uppercase():
  return (chr(random.randint(65,90)))

def lowercase():
  return (chr(random.randint(97,122)))

def digits():
  return (str(random.randint(0,9)))

def punctation():
  return (chr(random.randint(33,64)))

def pin4():
  return (str(random.randint(1111,9999)))

def shuffle(string):
  tempList = list(string)
  random.shuffle(tempList)
  return ''.join(tempList)

password = (uppercase()+lowercase()+digits()+punctation()+uppercase()+lowercase()+digits()+punctation())
password = shuffle(password)

while True:
  if button_a.was_pressed():
    display.scroll(pin4())
  elif button_b.was_pressed():
    display.scroll(password)
  else:
    display.show(Image.HEART)
Last edited by jimmo on Thu Apr 16, 2020 7:35 am, edited 1 time in total.
Reason: Add [code] formatting

User avatar
jimmo
Posts: 1633
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: micropython password generator help

Post by jimmo » Thu Apr 16, 2020 7:44 am

hi,

Unfortunately the micro:bit version of MicroPython doesn't implement random.shuffle.

Here's a simple implementation you can use:

Code: Select all

def random_shuffle(seq):
    l = len(seq)
    for i in range(l):
        j = random.randrange(l)
        seq[i], seq[j] = seq[j], seq[i]
FYI, as an Australian teacher, have you heard of Grok Learning and the Australian Computing Academy. They have a whole bunch of micro:bit / MicroPython resources, and due to COVID-19 they're currently making all their resources free. (And it was all already free for teachers anyway)

https://groklearning.com/COVID19-schools/
https://aca.edu.au/ (Some of the newer micro:bit courses are really cool)

(Disclaimer, I used to work for Grok, wrote their micro:bit simulator and worked on some of their earlier micro:bit courses)

koalaburger
Posts: 2
Joined: Thu Apr 16, 2020 4:43 am

Re: micropython password generator help

Post by koalaburger » Thu Apr 16, 2020 10:47 pm

Thanks for this mate - much appreciated!

Yep, we've been using grok learning this year, but only in this last few weeks have we really got everyone finally on board.

I can sort of make sense of your function - but I'm trying to work out how and where to use it...
Unfortunately, the micropython courses on Grok doesn't get into this much detail.

I think it's beyond my year 8s (and me) to do what you did... :lol:

User avatar
jimmo
Posts: 1633
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: micropython password generator help

Post by jimmo » Thu Apr 16, 2020 11:21 pm

Basically
koalaburger wrote:
Thu Apr 16, 2020 10:47 pm
I can sort of make sense of your function - but I'm trying to work out how and where to use it...
If you want to know more, it's a Fisher Yates shuffle, https://en.wikipedia.org/wiki/Fisher%E2 ... es_shuffle

Just chuck it somewhere in the file and then replace the line "random.shuffle(tempList)" with "random_shuffle(tempList)".
koalaburger wrote:
Thu Apr 16, 2020 10:47 pm
Unfortunately, the micropython courses on Grok doesn't get into this much detail.
Yeah, last time I checked there was one or two questions involving random.choice()... turns out it's quite hard to make the auto-marker work correctly when the program is allowed to use random, while still allowing all the different ways the student can solve the problem! :)
koalaburger wrote:
Thu Apr 16, 2020 10:47 pm
I think it's beyond my year 8s (and me) to do what you did...
Yeah, unfortunately MicroPython does have a bit of a cliff I guess when you hit the limits of what's built in. In this case I happened to know that micropython-lib has some extra helpers for random -- https://github.com/micropython/micropython-lib/ -- and so I adapted the code from there.

Post Reply