如何解决Python - 滚动两个边数不同的骰子
我已经为一个程序编写了代码,该程序从用户那里获取边数并将其中两个骰子掷出 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 个变量作为输入并从中生成随机数,也无需添加条件,您只需在其中指定选定的面数骰子本身
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()),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()),alpha=0.5)
plt.show()
示例:
$ python dice.py
What dice to use? (comma-separated,10): 8,10,24
<displays a plot>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。