python摇骰子函数 python 骰子
怎样python 写一个扑克和骰子的程序,模拟的5骰子的滚动,至多三次,具体要求如下:
参考下面的代码.
成都创新互联公司自2013年创立以来,是专业互联网技术服务公司,拥有项目成都网站制作、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元鄱阳做网站,已为上家服务,为鄱阳各地企业和个人服务,联系电话:18980820575
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,最多是骰子的最大值
python骰子问题
貌似不管是多少个色子和多少个面,X的数量应该只有一个,所以X=1,范围个数就是((a * b) - (a - 1)),所以函数定义中用不到X,试运行一下,看看是不是你想要的结果?
def dict(a, b):
x = 1 / ((a * b) - (a - 1))
return x
while True:
try:
A = int(input("请输色子入个数:"))
if A = 1:
B = int(input("请输色子入面数:"))
if B = 2:
C = int(input("请输入点数:"))
if C = A and C = A * B:
dict(A, B)
print('概率' + '%.2f%%' % (dict(A, B) * 100))
break
elif C = A * 1:
print("点数量要大等于色子个数,请重新输入。")
elif C A * B:
print("点数量要小于色子总数,请重新输入。")
elif B 2:
print("面数量要大于1,请重新输入。")
elif A 1:
print("色子数量要大于1,请重新输入。")
except ValueError:
print("格式不正确,请重新输入。")
python掷骰子游戏
# -*- coding: UTF-8 -*-
import random,time
def randstr(x):
num=int(random.uniform(1,7))
return [num,"第" + str(x) + "个骰子摇出来的点数是:" + str(num) + "\n"]
def tous(r):
sum,constr=0,""
# range(r) means 0 to r so use below
for i in range(1,r+1):
conresult=randstr(i)
sum+=conresult[0]
constr+=conresult[1]
return [sum,constr]
def calltous(k,v):
daxiao=("点数为小","点数为大")
result=tous(k)
print result[1] + "所有骰子摇得的总数是:" + str(result[0]) + "\n" + daxiao[(result[0]-v)0] + "\n"
while True:
calltous(3,10)
time.sleep(1.3)
分享题目:python摇骰子函数 python 骰子
URL链接:http://myzitong.com/article/doseiie.html