samedi 16 octobre 2021

Is there a way to store the result of a while loop (using the random module) inside a new variable - to be reused?

I want to grab the 5 random results from this function (will be five key-value pairs from a dictionary), and re-use them. Is there any way to do that?

def display_cards():
  i = 1
  while i < 6:
    print("")
    time.sleep(3)
    random_number = random.randint(0, len(flashcards))
    CARDNAME = "flash_" + str(random_number) 
    print(flashcards[CARDNAME])
    i += 1
display_cards()

An example result might be this (just to give an idea):

{'ingenjör (n)': 'Engineer'}

{'lärare (n)': 'Teacher'}

{'programmerare (n)': 'Programmer'}

{'försäljare (n)': 'Salesman'}

{'snickare (n)': 'Carpenter'}

These five results will be different each time the code is run (from a total of 100). The challenge is to grab and keep that random result and store it somehow in a variable, to re-use that combination of key-value pairs. Is it possible?

Here I will show the first few "flashcards" (nested dictionaries in a dictionary), to give an idea of how they are formatted:

flashcards = {"flash_0" : {"vecka (n)": "week"},
              "flash_1" : {"år (n)": "year"},
              "flash_2" : {"idag (adv)": "today"},
              "flash_3" : {"imorgon (adv)": "tomorrow"},
              "flash_4" : {"igår (adv)": "yesterday"},
              "flash_5" : {"kalender (n)": "calendar"},



Aucun commentaire:

Enregistrer un commentaire