【发布时间】:2020-07-13 17:12:39
【问题描述】:
因此,对于一个私人项目,我想将几个不同的骰子投掷数字化。到目前为止,我的代码要求输入,掷出多少和什么样的骰子,并输出结果。到目前为止,我有以下内容:
import dice
die = input("Roll the dice: ")
if die == "d4":
dice.d4()
elif die == "d6":
dice.d6()
而骰子模块包含
import random
def d4():
n = int(input())
x = 0
d4=[1, 2, 3, 4]
while x < n:
print(random.choice(d4))
x = x + 1
def d6():
n = int(input())
x = 0
d6=[1, 2, 3, 4, 5, 6]
while x < n:
print(random.choice(d6))
x = x + 1
等等。 用于确定骰子类型的 if-elif 检查似乎很实用,但感觉有些笨拙。 现在我的问题是:有没有更优雅的方法来检查骰子的数量和类型? 例如,我可以输入 3d6,脚本计算 3 次 d6-throw?
【问题讨论】: