Python Program That Simulates Rolling a 6 Sided Die and Adds Up the Result of Each Roll Till You Roll a 1

Python program that simulates rolling a 6 sided die and adds up the result of each roll till you roll a 1

If I understand rightly then you can simplify this quite a lot, like this:

import random
print("Well, hello there.")
score = 0
while score < 20:
a = random.randint(1,6)
print("A {} was rolled".format(a))
score += a
if a == 1:
print("Pigged out!")
score = 0
break
print("Turn score is {}".format(score))

python program that Simulates the rolling of two six-sided dice for 1000 times and stores the sum values in a file

Ok, to help with the frequency, you can use a dictionary which you should create before the while loop:

freq = {}

Then you can use str(result) as key in the dictionary which you increase if it already exists, and else create the key and set it to 1:

...
result = int(dice_roll) + int(dice_roll1)
key=str(result)
if key in freq:
freq[key] += 1 # increase if already exists
else:
freq[key] = 1 # create new
...

Once you have this dictionary, you should be able to calculate the mean, mode and median.

Program that simulates rolling a dice, and tells you how many times you roll each number

You need to put the line dice=random.randint(1,7) inside the for loop.



Related Topics



Leave a reply



Submit