import random
N = 5 # number of dice throws
values = [0] * N
for i in range(N):
values[i] = random.randint(1,6)
# Remove brackets
str_values = [str(i) for i in values] # convert to strings
new_values = ", ".join(str_values)
print("\nYou rerolled some dice and the new values are: {}".format(new_values))
样本输出:
You rerolled some dice and the new values are: 1, 1, 6, 1, 5
如果需要返回数组值的函数(所有3种类型),请使用以下命令:
import random
def calcVals(values, N):
for i in range(N):
values[i] = random.randint(1,6)
# Remove brackets
str_values = [str(i) for i in values] # convert to strings
new_values = ", ".join(str_values)
return values, str_values, new_values
N = 5 # number of dice throws
values = [0] * N
values, str_values, new_values = calcVals(values, N)
print("\nYou rerolled some dice and the new values are: {}".format(new_values))