-2

I have a list (23 items) of choices and I've transformed it into a dictionary in order to have a value for each choice.

I need to select 3 random choices from that dictionary each time I run the test and their value sum has to be 23.

Until now I did this:

list = ['somthing', 'something else'......'something else else']
nums = [1,2,3...23]
dic = dict(zip(lista, nums))

for x, y, z in dic:
    if dic.values(x) + dic.values(y) + dic.values(z) == 23:
        print(x, y, z)

and I get that there are too many values to unpack.

Any idea how I can get this done?

1
  • What did you expect for x, y, z in dic to do? Given the keys come from the first list, that's effectively e.g. x, y, z = 'something'. Commented Jun 20, 2018 at 6:32

1 Answer 1

0

Ok, so i got some help and i had to use a while statement. Posting the code bellow maybe someone will find it helpful.

while True:

key1 = random.choice(list(dic.keys()))
key2 = random.choice(list(dic.keys()))
key3 = random.choice(list(dic.keys()))

listSum = dic[key1] + dic[key2] + dic[key3]
if listSum == 23:
    print(key1, key2, key3)
    break
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.