Python怎么编程多少次骰子(python扔骰子用循环程序)
python 由用户输入掷多少次骰子,然后统计每个面出现的次数
#?coding=utf-8
import?random
n?睁做乱=?input('投多少次骰子悉档')
result?=?[]
for?i?胡旅in?range(n):
????x?=?random.randint(1,?6)
????print?('第%s次投骰子的结果为:?%s'?%?(i+1,?x))
????result.append(x)
for?i?in?range(6):
????print?('出现%s的次数为:?%s')?%?(i+1,?result.count(i?+?1))
用python语言。掷四个骰子,并记录其相应在点数之和。重复100次,展示每个结果相应在次数和。
import?random
l=[]
from?collections?import?Counter
for?i?雹昌in?range(100):
a=random.randint(1,6)
b=random.randint(1,6)
c=random.randint(1,6)
d=random.randint(1,6)
L=sum([a,b,c,d])
l.append(L)
c=Counter()
for?i?销肆旅in?l:
c[i]=c[i]+1
for?k,v?亏凳in?c.items():
print(k,v)
你好???? 想问一下怎么用Python编一个 同时投12个骰子 计算至少有两次
import?numpy?as?np
'''蒙特卡洛方法''毁仿陆'
epoch?=?10000?#?模拟10000次
count?=?0?#?计数
for?i?in?range(epoch):
???大灶?dice_12?=?np.random.randint(6,?size=12)?+?1?#?模拟每次投12个骰子
????if?dice_12[dice_12==6].size?=?2:?count?+=?1
print('同时投12个骰子,至少两个骰纤顷子六点的概率为{}'.format(count/epoch))
怎样python 写一个扑克和骰子的程序,模拟的5骰子的滚动,至多三次,具体要求如下:
参考下面的代码.
play 可能有问题,主要是没说清楚在保留牌的时候, 输入Ace 或者 "Ace Ace" 有什么区别,到底是输入一次 Ace 保留手上所有的 Ace 还是只保留一个,这个没说清楚。看例子,则顷颤这两种用法都有,我按照输入了几个就保留几个来做的。
simulate 没问题,和图片中的结果完全一样
必须用 python 3
import?random
import?collections
_dice_type?=?['Ace',?'King',?'Queen',?'Jack',?'10',?'9']
_hand_mapping?=?collections.OrderedDict([
????('5kind',????'Five?of?a?kind'),
????('4kind',????'Four?of?a?kind'),
????('full',?????'Full?house'),
????('straight',?'Straight'),
????('3kind',????'Three?of?a?孙败kind'),
????('2pair',????'Two?pair'),
????('1pair',????'One?pair'),
????('bust',?????'Bust'),
])
def?_check_hand(dices):
????counter?=?collections.Counter(dices)
????if?len(counter)?==?1:
????????return?'5kind'
????sorted5?=?counter.most_common(5)
????if?sorted5[0][1]?==?4:
????????return?'4kind'
????if?sorted5[0][1]?==?3:
????????if?sorted5[1][1]?==?2:
????????????return?'full'
????????else:
????????????return?'3kind'
????if?sorted5[0][1]?==?2:
????????if?sorted5[1][1]?==?2:
????????????return?'2pair'
????????else:
????????????return?'1pair'
????if?len(counter)?==?5:
????????dtype?=?sorted5[0][0]
????????for?x?in?sorted5:
????????????if?dtype?!=?x[0]:
????????????????break
????????????dtype?+=?1
????????else:
????????????return?'straight'
????????????
????return?'bust'
def?play():
????dices?=?[]
????retry?=?0
????while?True:
????????remain?=?5?-?len(dices)
????????if?remain?=?0:
????????????break
????????????
????????dices.extend([random.randint(0,5)?for?x?in?range(remain)])
????????
????????print("The?roll?is:?{}".format(
????????????"?".join([_dice_type[d]?for?d?in?sorted(dices)])
????????))
????????print("It?is?a?{}".format(_hand_mapping[_check_hand(dices)]))
????????if?retry??1:
????????????break
????????
????????prompt?=?"Which?dice?do?you?want?to?keep?for?the?{}?roll??".format(
????????????"second"?if?retry?==?0?else?"third"
????????)
????????while?True:
????????????answer?=?input(prompt).lower()
????????????if?answer?==?'all':
????????????????break
????????????answer?=?[x.capitalize()?for?x?in?answer.split()]
????????????if?set(answer).issubset(set(_dice_type)):
????????????????break
????????????print("That?is?not?possible,?try?again!")
????????retry?+=?1
????????if?answer?==?'all':
????????????print("Ok,?done")
????????????break
????????tmp?=?dices
????????dices?=?[]
????????for?x?in?tmp:
????????????乎此if?_dice_type[x]?in?answer:
????????????????dices.append(x)
????????????????answer.remove(_dice_type[x])
def?simulate(n,?debug=False):
????result?=?dict.fromkeys(_hand_mapping.keys(),?0)
????for?_?in?range(n):
????????dices?=?[random.randint(0,5)?for?x?in?range(5)]
????????if?debug:
????????????print("DEBUG:",?"?".join([_dice_type[d]?for?d?in?sorted(dices)]))
????????result[_check_hand(dices)]?+=?1
????for?k,?v?in?_hand_mapping.items():
????????cnt?=?result[k]
????????print("{:16s}:?{:.2f}%".format(v,?100*cnt/n))
python 掷骰子程序
一共有多少轮源个骰子,设为num个,然后执行randrange(sides)+1 num次,意思悔慧就是每个骰子做了一次投骰子的,然后拿到每次投筛子后的值。randrange(sides)+1 ,至腊前态少是1,最多是骰子的最大值