Python - 掷两个不同面数的骰子

Python - Rolling two dice with different numbers of sides

我已经编写了一个程序的代码,该程序从用户那里获取面数并将其中两个骰子掷 1000 次并在直方图上显示总和。

我如何做同样的事情,但用户可以选择不同的骰子,例如 6 面和 8 面?

这是我的类似骰子代码:

from random import randint
import matplotlib.pyplot as plt
from collections import defaultdict

#Asking for the sides
n = int(input("How many sides? 6, 8 or 10?\n"))
number = 1000 #How many times it's rolled

#Making empty dictionaries for each type of die
sums6 = defaultdict(int)
sums8 = defaultdict(int)
sums10 = defaultdict(int)

#Making conditions for each type and plotting the histograms
if n == 6:
    for _ in range(number):
        die1 = randint(1,6)
        die2 = randint(1,6)
        sums6[die1 + die2] += 1
    plt.bar(list(sums6.keys()), sums6.values(), color='b')
    plt.xlabel('Result')
    plt.ylabel('Frequency of Result')
    plt.grid(axis='y', alpha=0.5)
    plt.show()
if n == 8:
    for _ in range(number):
        die1 = randint(1,8)
        die2 = randint(1,8)
        sums8[die1 + die2] += 1
    plt.bar(list(sums8.keys()), sums8.values(), color='g')
    plt.xlabel('Result')
    plt.ylabel('Frequency of Result')
    plt.grid(axis='y', alpha=0.5)
    plt.show()
if n == 10:
    for _ in range(number):
        die1 = randint(1,10)
        die2 = randint(1,10)
        sums10[die1 + die2] += 1
    plt.bar(list(sums10.keys()), sums10.values(), color='r')
    plt.xlabel('Result')
    plt.ylabel('Frequency of Result')
    plt.grid(axis='y', alpha=0.5)
    plt.show()
from random import randint
import matplotlib.pyplot as plt
from collections import defaultdict

#Asking for the sides
n1, n2 = map(int, input("Enter a two values: ").split())

number = 1000 #How many times it's rolled

#Making empty dictionaries for each type of die
sums = defaultdict(int)


#Making conditions for each type and plotting the histograms
for _ in range(number):
    die1 = randint(1,n1)
    die2 = randint(1,n2)
    sums[die1 + die2] += 1
plt.bar(list(sums.keys()), sums.values(), color='b')
plt.xlabel('Result')
plt.ylabel('Frequency of Result')
plt.grid(axis='y', alpha=0.5)
plt.show()

如果你只希望用户能够输入一个骰子的多个面,你可以只需要2个变量作为输入并从中生成随机数,也不需要添加条件,你可以只指定选择的数量randint 本身的骰子面

from random import randint
import matplotlib.pyplot as plt
from collections import defaultdict

#Asking for the sides
n = int(input("How many sides Dice1? 6, 8 or 10?\n"))
m = int(input("How many sides Dice2? 6, 8 or 10?\n"))
number = 1000 #How many times it's rolled

#Making empty dictionaries for each type of die
sums = defaultdict(int)

#Making conditions for each type and plotting the histograms

for _ in range(number):
    die1 = randint(1,n)
    die2 = randint(1,m)
    sums[die1 + die2] += 1
plt.bar(list(sums.keys()), sums.values(), color='b')
plt.xlabel('Result')
plt.ylabel('Frequency of Result')
plt.grid(axis='y', alpha=0.5)
plt.show()

要求用户提供以逗号分隔的面数,然后使程序适用于具有任意面数的任意数量的骰子:

from collections import defaultdict
from random import randint

import matplotlib.pyplot as plt


NUMBER = 1000  # How many times it's rolled


# Asking for the sides
dice = input("What dice to use? (comma-separated, e.g. 6,8,10): ")
try:
    dice = [int(item) for item in dice.split(",")]
except ValueError:
    print("Error: input", dice, "is not comma-separated integers.")
    exit(1)

sums = defaultdict(int)

for _ in range(NUMBER):
    roll_result = [randint(1, m) for m in dice]
    sums[sum(roll_result)] += 1
plt.bar(list(sums.keys()), sums.values(), color='b')
plt.xlabel('Result')
plt.ylabel('Frequency of Result')
plt.grid(axis='y', alpha=0.5)
plt.show()

示例:

$ python dice.py
What dice to use? (comma-separated, e.g. 6,8,10): 8,8,10,24
<displays a plot>