1

I am trying to make a loop that can roll my dice and assign the rolled value into a list that can then be put through a dictionary to get back the values written in the dictionary. My dictionary is as follows:

scores = {1:1000, 2:200, 3:300, 4:400, 5:500, 6:600} rolls = [] for i in range(5): self.rolls.append(random.randint(1,6)) my_score = 0 

How do I make the values in rolls that of the ones in the dictionary and add them to my_score.

1
  • 1
    my_score += sum(scores[a] for a in rolls) ? Commented May 26, 2018 at 2:55

1 Answer 1

1
import random scores = {'1':1000, '2':200, '3':300, '4':400, '5':500, '6':600} results = 0 rolls = [] for i in range(5): rand_int = random.randint(1,6) if rand_int == 1: results += (scores['1']) elif rand_int == 2: results +=(scores['2']) elif rand_int == 3: results += (scores['3']) elif rand_int == 4: results +=(scores['4']) elif rand_int == 5: results += (scores['5']) elif rand_int == 6: results += (scores['6']) 

print(results)

change the dictionary key to string

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.