Reputation: 49
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)
Upvotes: 0
Views: 1421
Reputation: 250891
Instead of storing D20roll = random.randint(1,20)
in a variable, call it multiple times to get random results:
Using str.join
:
>>> D20 = 4
>>> print (", ".join(str(random.randint(1, 20)) for _ in range(D20)))
11, 4, 12, 4
Note that input()
returns a string in python3.x, so you need to call int()
on it:
sides = int(input())
D20 = int(input())
Upvotes: 1
Reputation: 32429
import random
sides = int (input ('Which die? ') )
count = int (input ('How many dice? ') )
print ( [random.randint (1, sides) for _ in range (count) ] )
Upvotes: 1