参考下面的代码
play 可能有问题,主要是没说清楚在保留牌的时候, 输入Ace 或者 "Ace Ace" 有什么区别,到底是输入一次 Ace 保留手上所有的 Ace 还是只保留一个,这个没说清楚。看例子,这两种用法都有,我按照输入了几个就保留几个来做的。
simulate 没问题,和中的结果完全一样
必须用 python 3
import randomimport collections
_dice_type = ['Ace', 'King', 'Queen', 'Jack', '10', '9']
_hand_mapping = collectionsOrderedDict([
('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 = collectionsCounter(dices)
if len(counter) == 1:
return '5kind'
sorted5 = countermost_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
dicesextend([randomrandint(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 = [xcapitalize() for x in answersplit()]
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:
dicesappend(x)
answerremove(_dice_type[x])
def simulate(n, debug=False):
result = dictfromkeys(_hand_mappingkeys(), 0)
for _ in range(n):
dices = [randomrandint(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_mappingitems():
cnt = result[k]
print("{:<16s}: {:2f}%"format(v, 100cnt/n))
#data和name_data数据自己添加
import random
# 所有员工数据,字典的key是一个set类型,value是list类型
data = {
('能永年', '甲队'): ['项目队长', '曾经外派抵达x地做出了某事', '曾经外派抵达子公司做出了ss项目'],
('巴伟毅', '乙队'): ['HR'],
('纪文博', '丙队'): ['项目成员', '为xx事做出了重大贡献', '曾经做过AA项目'],
('吉信鸿', '丙队'): ['HR', '为公司面试了XX人次', ],
('沙雅旭', '丙队'): ['财务管理', '针对财务的做出了重要指导'],
}
name_data = ['能永年', '巴伟毅', '纪文博', '吉信鸿', '沙雅旭'] # 参见抽奖人的姓名
random_name = randomchoice(name_data) # 随机一个中奖的人
for key, value in dataitems():
# print(key,value)
if random_name == key[0]: # 在字典key中找到这人
print('{}:{}'format(key[0], key[1]))
for item in value: # 遍历他的信息
print('\t\t' + itemljust(20))
n1=input('请输入苹果的数量')
n2=input('请输入橙子的数量')
s=int(n1)+int(n2)
print('计算结果为:',s)
运行结果截图
class motor1:
def set(self, number, speed: int, weight: int):
selfnumber = number
selfspeed = speed
selfweight = weight
def set_speed(self, num: int):
selfspeed = selfspeed + num
def pri(self):
print(selfnumber, selfspeed, selfweight)
class motor2:
def __init__(self, number, speed: int, weight: int):
selfnumber = number
selfspeed = speed
selfweight = weight
def set(self, number, speed: int, weight: int):
selfnumber = number
selfspeed = speed
selfweight = weight
def set_speed(self, num: int):
selfspeed = selfspeed + num
def pri(self):
print(selfnumber, selfspeed, selfweight)
if __name__ == '__main__':
a = motor1()
aset('辽A1234', 100, 50)
aset_speed(10)
apri()
b = motor2('辽A1234', 100, 50)
bset('辽B5678', 80, 20)
bset_speed(-20)
bpri()
以上就是关于怎样python 写一个扑克和骰子的程序,模拟的5骰子的滚动,至多三次,具体要求如下:全部的内容,包括:怎样python 写一个扑克和骰子的程序,模拟的5骰子的滚动,至多三次,具体要求如下:、python编写抽奖程序、计算机Python程序简单题目,求指点!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)