# 猜数字游戏
# 电脑随机出一个1-100的数字,电脑出数字,然后电脑1,2,3猜有没有猜中
# 电脑随机出一个1-100数字
import random
com1_score = [] # 电脑1分数 = [] 是个空列表
com2_score = []
com3_score = []
for com3 in range(100):
max_num = 1000000000 # 最大值 max
min_num = 1 # 最小值
we = random.randint(min_num, max_num) # 字符串
# print(computer)
# 我们猜一个数字 input
for i in range(1000):
# 人类策略:最小值和最大值中间出一个中间数,慢慢缩小范围
computer = random.randint(min_num, max_num) # 最小值和最大值中间出一个随机数
# computer = int((min_num + max_num)/2)
# 判断
if computer < we:
# print("电脑1猜小了")
min_num = computer+1
elif we < computer:
# print("电脑1猜大了")
max_num = computer-1
elif we == computer:
print("天选之脑1")
break
# print("电脑猜的是%d"%computer)
print("电脑1一共猜了%d局"%(i+1))
com1_score.append(i+1)
max_num = 1000000000 # 最大值 max
min_num = 1 # 最小值
for i in range(1000):
# 人类策略:最小值和最大值中间出一个出一个中间数,慢慢缩小范围
# compter 50
# computer = random.randint(min_num, max_num) # 最小值和最大值中间出一个随机数
computer = int((min_num + max_num)/2)
# 判断 15 16
if computer < we:
# print("电脑2猜小了")
min_num = computer
elif we < computer:
# print("电脑2猜大了")
max_num = computer
elif we == computer:
print("天选之脑2")
break
# print("电脑猜的是%d"%computer)
print("电脑2一共猜了%d局"%(i+1))
com2_score.append(i+1)
max_num = 100000000 # 最大值 max
min_num = 1 # 最小值
for i in range(1000):
# 人类策略:最小值和最大值中间出一个出一个中间数,慢慢缩小范围
# compter 50
# computer = random.randint(min_num, max_num) # 最小值和最大值中间出一个随机数
# computer = int((min_num + max_num)/2)
computer = random.randint(min_num, max_num)
# 判断 15 16
if computer < we:
# print("电脑2猜小了")
# min_num = computer
pass
elif we < computer:
# print("电脑2猜大了")
# max_num = computer
pass
elif we == computer:
print("天选之脑3")
break
# print("电脑猜的是%d"%computer)
print("电脑3一共猜了%d局"%(i+1))
com3_score.append(i+1)
print(com1_score)
print(com2_score)
print(com3_score)
# 数据分析
# 最大值
com1_max = 0
for i in com1_score:
if i > com1_max:
com1_max = i
print(com1_max)
# 最小值
# 平均值
# 出现次数最多的值
ziheng - 猜数字升级版
最新推荐文章于 2022-10-06 13:13:16 发布