|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是我们老师写的掷色子的代码,好几段没看懂。
import random
r1 = [] # random rolls dice 1
r2 = [] # random rolls dice 2
s = [] # sum of values
# 1 2 3 4 5 6
q1 = [0, 0, 0, 0, 0, 0] #
q2 = [0, 0, 0, 0, 0, 0] # roll value quantity
# 2 3 4 5 6 7 8 9 10 11 12
qs = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # sums quantity
for i in range(600):
r1.append(random.randint(1, 6))
r2.append(random.randint(1, 6))
for v in r1:
q1[v-1] = q1[v-1] + 1
for v in r2:
q2[v-1] = q2[v-1] + 1
print("rolls dice 1: ", r1)
print("counts dice 1: ", q1)
print("rolls dice 2: ", r2)
print("counts dice 2: ", q2)
for i in range(len(r1)): # or r2 'cause it is the same
s.append(r1[i] + r2[i])
for v in s:
qs[v-2] = qs[v-2] + 1
print("sums of rolls: ", s)
print("counts of sums: ", qs)
里面的:
for v in r1:
q1[v-1] = q1[v-1] + 1
for v in r2:
q2[v-1] = q2[v-1] + 1
和
for i in range(len(r1)): # or r2 'cause it is the same
s.append(r1[i] + r2[i])
for v in s:
qs[v-2] = qs[v-2] + 1
print("sums of rolls: ", s)
print("counts of sums: ", qs)
是什么意思呀?最后print的哪个qs又是啥呀?
|
|