嗨即时试图创造一个游戏,计算机生成1和6之间的5个随机数字,但我的问题是我已经创建了将获得一个列表'1'在各自的部分取决于什么数字出现。例如如果计算机产生31534列表需要显示[1,0,2,1,1,0](因为有2个3的它填补2中的3插槽)只显示5张随机数,并没有别的另一个函数中使用从一个函数的变量(参数“骰子”填充)
from random import randint
def rollDice():
dice = [str(randint(1, 6)) for _ in range(5)]
print(dice)
return dice
#-----------------------------------------------------------------
def countVals(dice):
totals = [0, 0, 0, 0, 0]
for x in dice:
if x == 1:
totals = totals[1] + 1
elif x == 2:
totals = totals[2] + 1
elif x == 3:
totals = totals[3] + 1
elif x == 4:
totals = totals[4] + 1
elif x == 5:
totals = totals[5] + 1
print(totals)
return totals
#------------------------------------------------------------------
rollDice()
countVals()
countVals(rollDice()),并从rollDice删除STR(),为什么你需要转换为字符串。 – 2014-11-25 00:11:44
请参阅http://www.python.org/dev/peps/pep-0008/代码样式准则。 – ThiefMaster 2014-11-29 12:13:55