为了换一种方式(更像是一种蟒蛇式的方式),下面是我的看法:
from random import randint
def roll_dice(n):
return [randint(1, 6) for _ in range(n)]
print('You re-rolled some dice and the new values are:',
', '.join(map(str, roll_dice(5)))
)
或者,如果你想要更好的可视化
print()
学生:
print('You re-rolled some dice and the new values are: ', end='')
print(*roll_dice(5), sep=', ')
最后,如果您不关心不以逗号分隔的值,您可以简单地:
print('You re-rolled some dice and the new values are:', *roll_dice(5))
这里是这个概念的一个证明:
Python 3.7.5 (default, Oct 17 2019, 12:16:48)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from random import randint
>>>
>>>
>>> def roll_dice(n):
... return [randint(1, 6) for _ in range(n)]
...
>>>
>>> print('You re-rolled some dice and the new values are:',
... ', '.join(map(str, roll_dice(5)))
... )
You re-rolled some dice and the new values are: 4, 3, 5, 5, 4
>>>