尝试像在地下城和龙中掷骰子但显示每个掷骰子。我不太清楚我做错了什么,并感谢所有的帮助。
from random import randint
def d(y): #basic die roll
return randint(1, y)
def die(x, y): #multiple die roll 2d20 could roll 13 and 7 being 20
for [x*d(y)]:
print (sum(int(y)))
print (die(3, 20))
好的,所以我接受了建议并改变了它,但仍然在我的回程线上收到错误
#
#trying to roll dice like in dungeons and dragon but display each roll
from random import randint
def d(sides):
return randint(1, sides)
def roll(n, sides):
return tuple(d(sides) for _ in range(n))
def dice(n, sides):
print (roll(n, sides))
return sum(dice)
print(dice(3,20))
答案 0 :(得分:1)
您只能将d()
一次通话的结果复用多次,您需要n
对d()
进行不同的调用:
from random import randint
def d(sides):
return randint(1, sides)
def roll(n, sides):
return tuple(d(sides) for _ in range(n))
dice = roll(3, 20)
print(dice, sum(dice))
# (20, 18, 1) 39
答案 1 :(得分:1)
注意:这是使用Python 3.6或更高版本
from random import randint
def roll(roll)
rolling = []
try:
for x in range(int(roll.split('d')[0])):
rolling.append(randint(1,int(roll.split('d')[1])))
except Exception as err:
print(f'I got bungled @_@ \n Error: {err}')
print(f'You rolled {" ".join(str(x) for x in rolling)} which has a total'
f' of {sum(rolling)}')
此功能的作用是将任何掷骰子组合(例如3d10、10d10等)接收,并使用fstring打印每个掷骰子,然后打印组合的总数。
编辑:f字符串可用于在必要时将其压缩得更多