vendredi 23 juillet 2021

How to add the sum of a dictionary if condition is true

I'm working on something similar to blackjack. There are only a few of them just for testing. I randomize two different key's from the dictionary. The problem I'm having is that I don't know how to add the total sum of the values based on the condition. I do understand that sum() doesn't work in this case, but I want something similar to get the returns from the for loop item, which returns two elements and add them together. Thank you in advance!

import random

numbers = {'9': 9, '10': 10, 'J': 10}
numbers_list = list(numbers.keys())

pick_one = random.choice(numbers_list)
pick_two = random.choice(numbers_list)

print(pick_one, pick_two)
for items in numbers:
    if pick_one in numbers_list and pick_two in numbers_list:
        if pick_one == items and pick_two == items:
            total = sum(numbers[items])
            print(total)



Aucun commentaire:

Enregistrer un commentaire