參考下面的代碼.
成都創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來(lái),是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站制作、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元鄱陽(yáng)做網(wǎng)站,已為上家服務(wù),為鄱陽(yáng)各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
play 可能有問(wèn)題,主要是沒(méi)說(shuō)清楚在保留牌的時(shí)候, 輸入Ace 或者 "Ace Ace" 有什么區(qū)別,到底是輸入一次 Ace 保留手上所有的 Ace 還是只保留一個(gè),這個(gè)沒(méi)說(shuō)清楚??蠢?,這兩種用法都有,我按照輸入了幾個(gè)就保留幾個(gè)來(lái)做的。
simulate 沒(méi)問(wèn)題,和圖片中的結(jié)果完全一樣
必須用 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))
一共有多少個(gè)骰子,設(shè)為num個(gè),然后執(zhí)行randrange(sides)+1 num次,意思就是每個(gè)骰子做了一次投骰子的,然后拿到每次投篩子后的值。randrange(sides)+1 ,至少是1,最多是骰子的最大值
貌似不管是多少個(gè)色子和多少個(gè)面,X的數(shù)量應(yīng)該只有一個(gè),所以X=1,范圍個(gè)數(shù)就是((a * b) - (a - 1)),所以函數(shù)定義中用不到X,試運(yùn)行一下,看看是不是你想要的結(jié)果?
def?dict(a,?b):
x?=?1?/?((a?*?b)?-?(a?-?1))
return?x
while?True:
try:
A?=?int(input("請(qǐng)輸色子入個(gè)數(shù):"))
if?A?=?1:
B?=?int(input("請(qǐng)輸色子入面數(shù):"))
if?B?=?2:
C?=?int(input("請(qǐng)輸入點(diǎn)數(shù):"))
if?C?=?A?and?C?=?A?*?B:
dict(A,?B)
print('概率'?+?'%.2f%%'?%?(dict(A,?B)?*?100))
break
elif?C?=?A?*?1:
print("點(diǎn)數(shù)量要大等于色子個(gè)數(shù),請(qǐng)重新輸入。")
elif?C??A?*?B:
print("點(diǎn)數(shù)量要小于色子總數(shù),請(qǐng)重新輸入。")
elif?B??2:
print("面數(shù)量要大于1,請(qǐng)重新輸入。")
elif?A??1:
print("色子數(shù)量要大于1,請(qǐng)重新輸入。")
except?ValueError:
print("格式不正確,請(qǐng)重新輸入。")
# -*- coding: UTF-8 -*-
import random,time
def randstr(x):
num=int(random.uniform(1,7))
return [num,"第" + str(x) + "個(gè)骰子搖出來(lái)的點(diǎn)數(shù)是:" + 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=("點(diǎn)數(shù)為小","點(diǎn)數(shù)為大")
result=tous(k)
print result[1] + "所有骰子搖得的總數(shù)是:" + str(result[0]) + "\n" + daxiao[(result[0]-v)0] + "\n"
while True:
calltous(3,10)
time.sleep(1.3)
當(dāng)前題目:python搖骰子函數(shù) python 骰子
新聞來(lái)源:http://jinyejixie.com/article4/doseiie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、搜索引擎優(yōu)化、微信公眾號(hào)、網(wǎng)站制作、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)