mardi 29 novembre 2022

Using Random Choice From Dictionary to Make An Image Appear?

Some preface:

I am using trinket. I am trying to make a tarot reading program for class. Using a nested dictionary, I have the name of each card, the facing of each card, and then a short (3-4 words) reading that is then outputted as a template for the user.

Example of nested dictionary:

card_names = {
"The Fool" : {"Up": "new beginnings, pleasure, & opportunity" , "Down" : "indecision, hesitation, & bad choices" }, 
  "The Magician" : {"Up" : "creativity, self-confidence, & skill" , "Down" : "delay, insecurity, & lack of self-confidence" }

I then use the random.choice function to generate both a card and it's facing, like so:

  card = random.choice(list(cards.card_names))
  facing = random.choice(list(cards.card_names[card]))

Then it outputs a reading based on answers users put in & the randomly generated card:

print(__main__.answer_list[0] + ", your past was represented by " + random.choice(list(cards.card_names)) + ", which is facing " + random.choice(list(cards.card_names[card])) + "." " This tells me that your past relationships brought " + (cards.card_names[card][facing]) + " into your life.")
  

it ends up looking something like in output: "

Em, your past was represented by The Chariot, which is facing Up. This tells me that your past relationships brought misery, greedy, & money into your life.

Now that I have the program actually working, I want to make it so that the random.choice cards that are chosen will actually show to the user, using files such as "tarot-fool.jpeg".

I made a second dictionary that looked like this:

card_images = {"The Fool": "tarot-fool.jpeg", "The Magician": "tarot-magician.jpeg"}

I hoped that I could have the program take the random.choice generated card from the card_names dictionary , parse the second dictionary for matches, and then once it found the card with matching keys, it would output the value of the second dictionary card_images, and then use the screen.bgpic() function to show users the card while they read their tarot reading output.

I tried something similar to this:

namesset = set(card_names)
imageset = set(card_images)

for name in namesset.intersection(card_images):
print name, card_images[name]

I also tried

card = random.choice(list(cards.card_names))
facing = random.choice(list(cards.card_names[card]))
key = card
key2 = cardimages.key()
value = cardimages.value()

if key in key2:
print value

just to see if I could get the program to isolate the correct value from card_images before I start trying to get them to show on screen, but I am unable to get the program to isolate the randomly selected card, remember it, and then parse the second dictionary for just that value. The code above works, but outputs every single name of every single .jpeg.

Please help! I honestly think this is something a little too complex to be using trinket for, especially with the images component. I have some experience coding outside of it but this is for a class project that we HAVE to turn in with trinket so if someone could either help me out or just tell me if this is not achievable through trinket so I can change direction a bit, it would be much appreciated.

see above for what I've tried




Aucun commentaire:

Enregistrer un commentaire