开发者

Python - Dice Simulator

开发者 https://www.devze.com 2023-03-17 18:41 出处:网络
This is my homework question: Write a program that simulates rolling a set of six-sided dice multiple times. The program should use a dictionary to record the results and then display the results.

This is my homework question:

Write a program that simulates rolling a set of six-sided dice multiple times. The program should use a dictionary to record the results and then display the results.

Input: The program should prompt for the number of dice to roll and the number of times to roll the dice.

Output:

The program is to display how many times each possible value was rolled. The format of the output must be as shown below:

The first column is the number shown on the dice when they are rolled. The brackets are only as wide as needed and the number inside the brackets is right justified. Note the minimum and maximum values in the sample runs below.

The second column is the number of times that value was rolled. This column is right justifie开发者_C百科d.

The last column is the percent of times that the number was rolled. The percentages are displayed with an accuracy of one decimal place.

This is the code I have so far:

import random
from math import floor, ceil
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
rand = float(0)
rolltotal = int(input("How many times do you want to roll? "))    
q = 0    
while q < rolltotal:
    q = q + 1
    rand = ceil(6*(random.random()))
    if rand == 1:    
        one = one + 1
    elif rand == 2:
        two = two + 1
    elif rand == 3:
        three = three + 1
    elif rand == 4:
        four = four + 1
    elif rand == 5:
        five = five + 1
    else:
        six = six + 1

total = one + two + three + four + five + six

print("[1]", one, " ",round(100*one/total, 1),"%")    
print("[2]", two, " ",round(100*two/total, 1),"%")
print("[3]", three, " ",round(100*three/total, 1),"%")
print("[4]", four, " ",round(100*four/total, 1),"%")
print("[5]", five, " ",round(100*five/total, 1),"%")    
print("[6]", six, " ",round(100*six/total, 1),"%")

My question is: I just know how to roll one dice. how can i get more than one .


from collections import defaultdict
import random

dice = int(input("How many dice do you want to roll? "))
rolls = int(input("How many times do you want to roll them? "))

irange = xrange
sides = [1,2,3,4,5,6]

d = defaultdict(int)
for r in irange(rolls):
    d[sum( random.choice(sides) for d in irange(dice) )] += 1

total = float(rolls)
for k in sorted(d.keys()):
    print "[%d] %d %.1f%%" % (k, d[k], 100.0*d[k]/total)


You would rather do the following:-

loop=True
import random  #This allows python to import built in "random" function.
while loop is True:    
    dice=input("Which sided dice would you like to roll? (You can only choose 4,6 or 12) \n") # "\n" allows python to add a new line.
    if dice=="4":
         print("You have chosen to roll dice 4\nYour score is... \n", random.randint(1,4)) #This allows python to generate a random number from 1 to 4.
    elif dice=="6":
         print("You have chosen to roll dice 6\nYour score is...\n", random.randint(1,6)) #This allows python to generate a random number from 1 to 6.
    elif dice=="12":
        print("You have chosen to roll dice 12\nYour score is...\n", random.randint(1,12)) #This allows python to generate a random number from 1 to 12.
    else: print("Invalid option! Please try again!") 
    loop2=input("Do you want to roll again? (Yes or No) \n")
    if loop2=="yes" or loop2=="Yes": # "or" funtion allows python to accept any of the two answers input by the user.
       loop=True
    else: break    # This function allows program to close.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号