-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdice_roll.py
More file actions
64 lines (53 loc) · 2.24 KB
/
dice_roll.py
File metadata and controls
64 lines (53 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
################################################################################
#
# Program: Dice Roll Simulator
#
# Description: Example of how to simulate a dice roll in Python, for any number
# of dice and any number of sides per dice.
#
# YouTube Lesson: https://www.youtube.com/watch?v=bsIYU_q5g7Y
#
# Author: Kevin Browne @ https://portfoliocourses.com
#
################################################################################
# We import the random module so we can use the randint() function to generate
# random integers in a given range
import random
# Returns a list containing the results of a dice roll, where we have dice
# number of elements set to randomly selected values between 1 ... sides
def dice_roll(dice, sides):
# We start off with an empty list
roll = []
# We create a loop that will execute 'dice' number of times, to roll each
# dice. We use the randint() method of the random module to obtain a random
# integer between 1 and sides, the 'face' of the dice that has been 'rolled'.
# We then append this new result to the roll list.
for i in range(0,dice):
face = random.randint(1,sides)
roll.append(face)
# Return the now complete list of dice roll results
return roll
# Prompt the user to enter the number of dice to be rolled, convert the string
# entered into an int, and store the int into the dice variable
dice = int(input("Dice: "))
# We can't roll the dice if there isn't at lesat one dice, so if the dice
# integer is less than or equal to zero we exit the program with an error
# message.
if (dice <= 0):
print("Must have at least one dice!")
quit()
# Prompt the user to enter the number of sides per dice, convert the string
# entered into an int, and store the int into the sides variable
sides = int(input("Sides: "))
# We can't roll a dice if there isn't at least 1 side, so if the sides
# integer is less than or equal to zero we exit the program with an error
# message.
if (sides <= 0):
print("Must have at least one side!")
quit()
# Perform the dice roll using the dice_roll() function, passing the dice and
# sides value provided by the user as arguments, assign the list returned to
# roll
roll = dice_roll(dice, sides)
# Print out the result of the dice roll
print(roll)