-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?

  • 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'. – jonrsharpe Jun 20 '18 at 6:32
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.