0

I am new to python and self taught. I would like to create a simple Dice rolling program but the problem I'm having is How to display more than one random integer I know I have to specify the dice roll as an integer but I'm not sure how Ill put part of the code below. # 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)
4

2 回答 2

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())
于 2013-09-14T08:28:39.087 回答
1
import random

sides = int (input ('Which die? ') )
count = int (input ('How many dice? ') )
print ( [random.randint (1, sides) for _ in range (count) ] )
于 2013-09-14T08:29:47.113 回答