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

标签 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 中。

最佳答案

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'])

打印(结果)

将字典键更改为字符串

关于python - 如何将列表中的值分配给字典并返回到单独的列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50538887/

相关文章:

python - Statsmodels 重复图表?

python - 列表列表中的平均列表 - 有更有效的方法吗?

python - 在 Python 中,deepcopy 和 [each[ :] for each in List] 有什么区别

java - 写入数组

arrays - Dart:如何将简单的 map 转换为 dart/flutter 中的列表?

python - 从嵌套字典中删除元组键;避免运行时和类型错误

python - 处理带有重复键的 JSON

python - 如何在train_test_split中选择RandomState?

python - 在 Tkinter、Python 3 中设置窗口尺寸

java - 使用StanfordCoreNLPServer将props(属性)从Java转换为Python3