import random
def roll():
min_value = 1
max_value = 6
roll = random.randint(min_value, max_value)
return roll
print("Roll the dice! (press enter)")
player = roll()
print(player)
The concept you are looking for is a loop.
I think what you want to do is call the roll() six times.
For this you can use a for loop.
for i in range(6):
print(i)
This should get you going.
Unclear what you want. What does “make this roll 6 dice in one roll” mean, and how do you judge what’s “best”?
@Snowcap1659, are you familiar with any of the following Python features?:
- lists
for
loops- list comprehensions
Try to incorporate a portion of this statement from your code into a for
loop or list comprehension that enables your function to return a list of the results of six independent dice rolls, instead of from only one dice roll:
roll = random.randint(min_value, max_value)
Among various means of learning how to program, there is the strategy of taking an example of code that does not do exactly what is required, but which instead performs a similar task, and then adapting that code to the specified need.
Your dice consist of six cubes, each of which has six square faces of equal size. Some games use dice with other numbers of faces, such as four, eight, twelve, or twenty.
A regular icosahedron is a geometric solid with twenty faces of equal size, each of which is an equilateral triangle. The code below simulates a roll of twelve dice of that form. It makes use of a list comprehension to create a list representing the results of that roll.
Now you have several options, including writing your function from scratch, revising the for
loop provided above by @barry-scott, or adapting and incorporating the list comprehension below into your function.
from random import randint
num_dice = 12
num_faces = 20 # icosahedron
roll = [randint(1, num_faces) for _ in range(num_dice)]
print(roll)
Example output:
[3, 9, 20, 3, 8, 1, 13, 16, 17, 13, 9, 3]
Please let us know how this works out for you. It would be great to see your resulting code.
There’s also the random.choices
function, which conveniently produces however many number of random choices drawn from a fixed population:
random.choices(range(min_value, max_value + 1), k=6)