Dice Rolling Simulator
Maybe use a for
-loop and iterate over the number
of times the user wants to roll the dice, while saving those to a list to display each roll of the die.
For example, the first die may look like this:
rolls = []
if dice == 'd20':
for roll in range(number):
rolls.append(random.randint(1,21))
print('rolls: ', ', '.join([str(roll) for roll in rolls]))
print('total:', sum(rolls))
Example output:
What kind of dice would you like to roll?: d20
How many times?: 2
rolls: 10, 15
total: 25
How to Make Dice Rolling in Python3
First, you will have to import a 'random' number using the 'random' number module. So the randint is a function which basically stands for Random Integer. THen you will define your functions and pass the range of numbers from which the random number will come from (0,6). So repeat=True basically helps you to play the game another more time while the .lower will convert the uppercase to the lower case in that one can choose to play the game while the caps lock is on or off.
#Dice Rolling Simulator#dice.py
from random import randint
def rand(): return randint(0,6) repeat = Truewhile repeat: print("You rolled",rand()) print("Do you want to roll again?") repeat = ("y" or "yes") in input().lower()print("Game Over")
Python dice rolling simulation
You have a small typo; you are testing for equality, not assigning:
four == four+1
should be:
four = four+1
However, you already have a number between 1 and 6, why not make that into an index into the results list? That way you don't have to use so many if
statements. Keep your data out of your variable names:
def rollDie(number):
counts = [0] * 6
for i in range(number):
roll = random.randint(1,6)
counts[roll - 1] += 1
return counts
Rolling 4 dices 1000 times and counting the number of times the sum of the four dices' score is equal to 21 or higher
I fixed the problem. Can you try this
import random
n = 0
for i in range(1000):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
dice3 = random.randint(1,6)
dice4 = random.randint(1,6)
sum = dice1 + dice2 + dice3 + dice4
if sum >= 21:
n = n + 1
print("You got 21 or higher", n, "times.")
Simulate rolling 2 dice 24 times and calculating the probability of getting a 6 in Python
random.randint(a,b)
includes the endpoints a
and b
, so use random.randint(1,6)
.
Assuming you meant double 6s in the second case, you are counting double 6s more than once per trial. Compute all 24 and then check for any instance of double 6s.
Here is working code (Python 3.6):
from random import randint
trials = 1000
total = 0
for i in range(trials):
if 6 in [randint(1,6) for j in range(4)]:
total +=1
print(f'A 6 appeared when rolling 1 die 4 times {total/trials:.2%} of the time.')
total = 0
for i in range(trials):
if (6,6) in [(randint(1,6),randint(1,6)) for j in range(24)]:
total +=1
print(f'Double 6s appeared when rolling 2 dice 24 times {total/trials:.2%} of the time.')
Output:
A 6 appeared when rolling 1 die 4 times 50.30% of the time.
Double 6s appeared when rolling 2 dice 24 times 48.90% of the time.
Related Topics
How to Save Opened Page as Pdf in Selenium (Python)
Read Multiple Images on a Folder in Opencv (Python)
Replace a Word in a String by Indexing Without "String Replace Function" -Python
How to Delete Lines from CSV File Using Python
Python: Import Cx_Oracle Importerror: No Module Named Cx_Oracle Error Is Thown
Typeerror: Image Data Can Not Convert to Float
Python/Regex - How to Extract Date from Filename Using Regular Expression
Python Pandas: Nameerror: Name Is Not Defined
Paramiko Capturing Command Output
Python: Printing Horizontally Rather Than Current Default Printing
Valueerror: Feature_Names Mismatch: in Xgboost in the Predict() Function
Xlsxwriter: How to Open an Existing Worksheet in My Workbook
How to Calculate Range Between the Dataframe Values Using Python
How to Write Python Array (Data = []) to Excel
Concatenate Two Columns in Csv: Python