我是python和自学的新手。我想创建一个简单的Dice滚动程序,但我遇到的问题是如何显示多个随机整数我知道我必须将骰子滚动指定为整数但我不确定我是如何放置部分的代码如下。 #D& D Dice Roller
import random
import time
print("What dice would you like to roll")
sides = input()
if sides == 20:
D20roll = random.randint(1,20)
print ("How many dice would you like to roll")
D20 = input()
if D20 == 1:
print(D20roll)
if D20 == 2:
print(D20roll + "," + D20roll)
if D20 == 3:
print(D20roll + "," + D20roll + "," +D20roll)
答案 0 :(得分:1)
不是将D20roll = random.randint(1,20)
存储在变量中,而是多次调用以获得随机结果:
使用str.join
:
>>> D20 = 4
>>> print (", ".join(str(random.randint(1, 20)) for _ in range(D20)))
11, 4, 12, 4
请注意,input()
会在python3.x中返回一个字符串,因此您需要在其上调用int()
:
sides = int(input())
D20 = int(input())
答案 1 :(得分:1)
import random
sides = int (input ('Which die? ') )
count = int (input ('How many dice? ') )
print ( [random.randint (1, sides) for _ in range (count) ] )