当前位置: 首页 > news >正文

python 练习

题目

  1. 当输入单人赢球概率不在1到0之内,提示重新输入
  2. 每局比赛一方比分大于21,且与另一方分差至少为2,则该场胜利者为得分高者,否者得分高者仅为该局胜利者
  3. 每场比赛三局两胜,先赢两场为胜利者

代码

import random# 判断是否为数字
def is_number(v):if v.isdigit():return float(v)else:try:if float(v):return float(v)else:return Falseexcept ValueError:return False# 获取单人成绩 随机数    
def getr(p):s = random.triangular(0, p)*100s = round(s)return s# 获取单局结果
def def_result_item(ap,bp):a_s,b_s = getr(ap),getr(bp)# a_s,b_s = 7,11max_s = 21s_list = {"a":a_s,"b":b_s}ss_list = sorted(s_list.items(), key=lambda x:x[1], reverse=True)k0,v0 = ss_list[0]k1,v1 = ss_list[1]r = Falseif v1!=v0:if v0>=21 and v0-v1>=2:r=k0else:print(ss_list)return Falsedata = {"list":ss_list,"r":r}return data#获取单场结果
def get_result(ap,bp):last_r = Falseresult_list = {}result = ""result_list =[]for i in range(1,3):data = def_result_item(ap,bp)if not data:continuer = data.get("r")list_v = data.get("list")result_data = {"index":i,'r':r,"list":list_v}result_list.append(result_data)if r:last_r = rbreak;k,v= list_v[0]result += k+","ca = result.count('a')cb = result.count('b')if ca==2:last_r =  'a'breakif cb==2:last_r = 'b'breakdata = {"list":result_list,"r":last_r}return data#调用    
def get_inputs(times):pa = input("输入单球获胜概率")while True:pa=is_number(pa)if pa:if not (pa>0 and pa<1):pa = input("输入错误,请重新输入")else:breakelse:pa = input("输入数字,请重新输入")pb=round(1-pa,2)print("a p:{0} b p:{1} itmes:{2}".format(pa,pb,times))ra = 0rb = 0for i in range(0,times):print("time:{0}".format(i))result = get_result(pa,pb)r =  result.get("r")list_v = result.get("list")print(list_v)if r =="a":ra+=1if r =="b":rb+=1print("ra:{0} tb:{1}".format(ra,rb))#比赛50次
get_inputs(50)

输出

输入单球获胜概率 aa
输入数字,请重新输入 0.4
a p:0.4 b p:0.6 itmes:50
time:0
[{'index': 1, 'r': 'b', 'list': [('b', 40), ('a', 24)]}]
time:1
[{'index': 1, 'r': 'a', 'list': [('a', 37), ('b', 4)]}]
time:2
[{'index': 1, 'r': 'b', 'list': [('b', 27), ('a', 13)]}]
time:3
[{'index': 1, 'r': False, 'list': [('b', 18), ('a', 15)]}, {'index': 2, 'r': 'a', 'list': [('a', 34), ('b', 22)]}]
time:4
[{'index': 1, 'r': 'b', 'list': [('b', 46), ('a', 36)]}]
time:5
[{'index': 1, 'r': 'a', 'list': [('a', 23), ('b', 16)]}]
time:6
[{'index': 1, 'r': 'b', 'list': [('b', 38), ('a', 19)]}]
time:7
[{'index': 1, 'r': 'b', 'list': [('b', 23), ('a', 13)]}]
time:8
[{'index': 1, 'r': 'b', 'list': [('b', 32), ('a', 22)]}]
time:9
[{'index': 1, 'r': 'b', 'list': [('b', 33), ('a', 30)]}]
time:10
[{'index': 1, 'r': False, 'list': [('b', 20), ('a', 18)]}, {'index': 2, 'r': 'b', 'list': [('b', 51), ('a', 10)]}]
time:11
[{'index': 1, 'r': 'b', 'list': [('b', 40), ('a', 22)]}]
time:12
[{'index': 1, 'r': 'b', 'list': [('b', 41), ('a', 5)]}]
time:13
[{'index': 1, 'r': 'b', 'list': [('b', 25), ('a', 18)]}]
time:14
[{'index': 1, 'r': False, 'list': [('a', 17), ('b', 11)]}, {'index': 2, 'r': 'b', 'list': [('b', 40), ('a', 8)]}]
time:15
[{'index': 1, 'r': 'a', 'list': [('a', 27), ('b', 17)]}]
time:16
[('a', 33), ('b', 33)]
[{'index': 2, 'r': 'a', 'list': [('a', 26), ('b', 22)]}]
time:17
[{'index': 1, 'r': 'b', 'list': [('b', 34), ('a', 23)]}]
time:18
[{'index': 1, 'r': 'a', 'list': [('a', 22), ('b', 7)]}]
time:19
[{'index': 1, 'r': 'a', 'list': [('a', 28), ('b', 25)]}]
time:20
[{'index': 1, 'r': 'b', 'list': [('b', 27), ('a', 10)]}]
time:21
[{'index': 1, 'r': 'b', 'list': [('b', 34), ('a', 11)]}]
time:22
[('a', 28), ('b', 28)]
[{'index': 2, 'r': 'b', 'list': [('b', 43), ('a', 6)]}]
time:23
[{'index': 1, 'r': 'b', 'list': [('b', 24), ('a', 17)]}]
time:24
[{'index': 1, 'r': 'b', 'list': [('b', 39), ('a', 23)]}]
time:25
[{'index': 1, 'r': 'b', 'list': [('b', 46), ('a', 25)]}]
time:26
[{'index': 1, 'r': 'b', 'list': [('b', 44), ('a', 11)]}]
time:27
[{'index': 1, 'r': 'b', 'list': [('b', 44), ('a', 6)]}]
time:28
[{'index': 1, 'r': 'b', 'list': [('b', 48), ('a', 8)]}]
time:29
[{'index': 1, 'r': 'b', 'list': [('b', 41), ('a', 32)]}]
time:30
[{'index': 1, 'r': 'b', 'list': [('b', 30), ('a', 10)]}]
time:31
[{'index': 1, 'r': 'b', 'list': [('b', 39), ('a', 14)]}]
time:32
[{'index': 1, 'r': 'b', 'list': [('b', 46), ('a', 17)]}]
time:33
[{'index': 1, 'r': 'a', 'list': [('a', 34), ('b', 6)]}]
time:34
[{'index': 1, 'r': 'b', 'list': [('b', 34), ('a', 16)]}]
time:35
[{'index': 1, 'r': 'b', 'list': [('b', 28), ('a', 16)]}]
time:36
[{'index': 1, 'r': 'b', 'list': [('b', 49), ('a', 33)]}]
time:37
[{'index': 1, 'r': 'b', 'list': [('b', 31), ('a', 5)]}]
time:38
[{'index': 1, 'r': 'b', 'list': [('b', 24), ('a', 11)]}]
time:39
[{'index': 1, 'r': 'a', 'list': [('a', 22), ('b', 17)]}]
time:40
[{'index': 1, 'r': 'b', 'list': [('b', 28), ('a', 20)]}]
time:41
[{'index': 1, 'r': 'b', 'list': [('b', 45), ('a', 20)]}]
time:42
[{'index': 1, 'r': 'b', 'list': [('b', 24), ('a', 18)]}]
time:43
[{'index': 1, 'r': 'b', 'list': [('b', 46), ('a', 27)]}]
time:44
[{'index': 1, 'r': False, 'list': [('a', 11), ('b', 8)]}, {'index': 2, 'r': 'b', 'list': [('b', 34), ('a', 13)]}]
time:45
[{'index': 1, 'r': 'a', 'list': [('a', 29), ('b', 26)]}]
time:46
[{'index': 1, 'r': 'a', 'list': [('a', 21), ('b', 15)]}]
time:47
[{'index': 1, 'r': 'b', 'list': [('b', 29), ('a', 19)]}]
time:48
[{'index': 1, 'r': 'b', 'list': [('b', 41), ('a', 34)]}]
time:49
[{'index': 1, 'r': False, 'list': [('a', 20), ('b', 19)]}, {'index': 2, 'r': False, 'list': [('b', 16), ('a', 14)]}]
ra:11 tb:38

相关文章:

  • 基于LoRA的Llama 2二次预训练实践:高效低成本的大模型领域适配
  • 使用c++调用deepseek的api(附带源码)
  • AI律师匹配AI分析法律需求意图并匹配律师
  • 为什么在TCP层(即传输层)没有解决半包、粘包的问题
  • 基于SpringBoot的在线抽奖系统测试用例报告
  • unity之协程
  • 算法学习(二)
  • 【Leetcode 每日一题】2145. 统计隐藏数组数目
  • 爬虫学习——LinkEXtractor提取链接与Exporter导出数据
  • OpenCV 图形API(47)颜色空间转换-----将 I420(YUV 4:2:0) 格式的图像数据转换为 RGB 格式
  • 大模型应用开发大纲
  • 什么是SPA,SPA与MAP区别
  • alibaba-JSONObject使用
  • 数据库优化
  • 全球首个人形机器人半程马拉松技术分析:翻车名场面背后的突破与挑战
  • Java第五节:继承thread类创建线程
  • C语言高频面试题——strcpy与memcpy区别
  • 江湖密码术:Rust中的 bcrypt 加密秘籍
  • NestJS-Knife4j
  • Java Lambda表达式指南
  • 又一名被拐孩子找到!29年后,在警方帮助下云南男子寻子成功
  • 协信远创620亿元债务重整计划获法院批准:冯仑入局,部分核心资产已提前转让
  • 调查显示特朗普在经济问题上的支持率跌至其总统生涯最低
  • 2025上海半马鸣枪,多个“首次”冲击一城双白金
  • 解读丨连续两日施压,特朗普为何着急让美联储降息
  • 两大跨国巨头称霸GLP-1市场,国产减肥药的机会在哪?