# 猜数字游戏
# 电脑出一个1-100的数字,你猜一下看能不能猜中
# 1.电脑是不是得随机出一个和数字
# 1.优化:告诉我几次猜中数字
# 2.优化:告诉我猜的范围
import random
score = []
for ii in range(100):
a = 0
max_num = 1000 # 最大值
# min 最小值
min_num = 0
we = random.randint(min_num, max_num)
for i in range(200):
# 电脑出数字的策略:1-100随机出
print("%d-%d" % (min_num, max_num))
# computer1 = random.randint(1, 100) # 数字
computer = random.randint(min_num, max_num) # 数字
# 人类策略
# computer = int((min_num+max_num)/2)
a = a + 1
# 3。判断
if computer == we:
print("电脑的战场是武汉")
break
elif computer > we:
print("电脑猜的大了")
max_num = computer
elif computer < we:
print("电脑猜的小了")
min_num = computer
print("电脑一共猜了%d 次" %(ii+1))
score.append(a)
print(score)
# 最大值
# 先设定一个最大值
big = 10
# 然后和列里的数字一个个比较看谁大
for i in score:
if i > big:
big = i
print("猜的最多次数是%d次"% big)
# 最小值 small
small=22
for i in score:
if i < small:
small = i
print("猜的最少次数是%d次"% small)
# 平均分
zaa = 0
for i in score:
zaa = zaa+i
print(zaa)
print(zaa/len(score))
xingtai - 求最大值,最小值和平均数
最新推荐文章于 2022-06-16 23:42:46 发布