如何将列表中的值分配给字典并返回单独的列表?

时间:2018-05-26 02:52:02

标签: python python-3.x list dictionary

我正在尝试创建一个可以滚动我的骰子并将滚动值分配到列表中的循环,然后可以通过字典将其放回到字典中写入的值。 我的字典如下:

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

如何将卷中的值与字典中的值进行比较并将其添加到my_score

1 个答案:

答案 0 :(得分: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'])

打印(结果)

将字典键更改为字符串