How to randomize the card selection

Hey fellow learners.
I thought of a fun twist to this interesting exercise - I thought - it would be much more thrilling if the cards were actually randomly assigned, rather than written in by the corresponding key.

I’ve written and run the code, myy result was:
Your past is the Justice card.
Your present is the Strength card.
Your future is the The Magician card.

You can try to write the code on your own, here are some tips:

  • We need numpy.py - we’ve used this library before. We can bring its functionality into our program with a command: import numpy as np . As a reminder numpy is a mathematic library with a bunch of useful functions.
  • numpy.random.choice() will be the most suitable option to choose the card. It takes a list of numbers and returns a random choice from that list.
  • In order to retreive each card will need to use the list of all available card indices, which hold all the currently present keys in the tarot list as an argument for np.random.choice(). Convenient for us, once the card is ‘popped’ it disappears from the list together with its index, and if we update the list each time np.random.choice() won’t consider that index again.

If you’re stuck somewhere or it’s too difficult here’s the code:

tarot = { 1:	"The Magician", 2:	"The High Priestess", 3:	"The Empress", 4:	"The Emperor", 5:	"The Hierophant", 6:	"The Lovers", 7:	"The Chariot", 8:	"Strength", 9:	"The Hermit", 10:	"Wheel of Fortune", 11:	"Justice", 12:	"The Hanged Man", 13:	"Death", 14:	"Temperance", 15:	"The Devil", 16:	"The Tower", 17:	"The Star", 18:	"The Moon", 19:	"The Sun", 20:	"Judgement", 21:	"The World", 22: "The Fool"}
#importing numpy
import numpy as np
spread = {}

#following loop updates the available card list each turn 
#and creates needed dictionary entries, while removing used cards
for each in ['past','present','future']:
  cards_left = list(tarot.keys())
  spread[each] = tarot.pop(np.random.choice(cards_left))
  
for key, value in spread.items():
  print("Your {} is the {} card.".format(key, value))

Yours is a much cleaner method than what I came up with, but I haven’t gone through a Numpy course (yet). I know of the random library though , so I came up with the below.

# Create a list of 3 random numbers to pull for the tarot cards.
random_lst = []
for i in range(3):
  rand_num = random.randint(1,22)
  while rand_num in random_lst:
    rand_num = random.randint(1,22)
  random_lst.append(rand_num)

spread = {}
spread['past'] = tarot.pop(random_lst[0])
spread['present'] = tarot.pop(random_lst[1])
spread['future'] = tarot.pop(random_lst[2])

I am using the base code that the project used, and just randomly coming up with which key’s to pop.

You came up with that just with the knowledge from this course or did you had some other coding experience? I’m asking cause there’s no way I could do it and I’m wondering if I’m just stupid or people like you had some other experiences that learned them how to think like a programmer.

Man, I don’t even fully understand what your code is doing :confused:

2 Likes

My code is doing the following.

  1. Generating a random number between 1 and 22
  2. Verifying that that number hasn’t already been selected by seeing if it is in ‘random_lst’
  3. Adding the new number to the ‘random_lst’
    After the above is complete I have the ‘random_lst’ list with 3 different and random numbers that I can use as key’s to pull the values with from the tarot dictionary.

Everything except maybe the random library has been covered up to this point.

I have limited coding experience from my job, but all that experience is in C++ and is very basic automation control. With that said, I do have an technical degree that required a couple programming classes, but nothing overly advanced.

Just keep with it and you’ll get there.

1 Like

I don’t understand why we need numpy.
I just uses random and it works. Is there a specific reason you use it?

2 Likes

numpy library has a larger variety of mathematical functions, thus is more preferred generally speaking instead of adding for example the random and sqrt function from separate modules.

1 Like
import random

tarot = { 1:	"The Magician", 2:	"The High Priestess", 3:	"The Empress", 4:	"The Emperor", 5:	"The Hierophant", 6:	"The Lovers", 7:	"The Chariot", 8:	"Strength", 9:	"The Hermit", 10:	"Wheel of Fortune", 11:	"Justice", 12:	"The Hanged Man", 13:	"Death", 14:	"Temperance", 15:	"The Devil", 16:	"The Tower", 17:	"The Star", 18:	"The Moon", 19:	"The Sun", 20:	"Judgement", 21:	"The World", 22: "The Fool"}

def tarot_card():
  spread = {}
  spread["past"] = tarot.pop(random.choice(list(tarot)))
  spread["present"] = tarot.pop(random.choice(list(tarot)))
  spread["future"] = tarot.pop(random.choice(list(tarot)))
  for key, value in spread.items():
    print("Your " + key +" is the " + str(value) + " card." )

tarot_card()
5 Likes

good stuff. simple, clean, easy to read and understand. well done.

1 Like
import random
spread= {}

for i in ["past", "present", "future"]:
  card_left = list(tarot.keys())
  spread[i] = tarot.pop(random.choice(card_left))
  print("Your {} is the {} card.".format(i, spread.get(i)))
1 Like