shanzhi -猜数字游戏

import random
import numpy as np
# 我们出数字
we = int(input("请出一个数字让电脑来猜"))
if 1<= we <= 10000:
    # 策略:取中间数
    max_num = 10000
    min_num = 1
    for i in range(10000):
        # 它猜一个数字
        computer = np.floor((max_num - min_num)/2+min_num)
        #  random.randint(min_num, max_num)
        print("第%d次电脑出了%d"%(i+1, computer))
        # 判断
        if computer > we:
            print("电脑猜大了")
            max_num = computer-1
        elif we > computer:
            print("电脑猜小了")
            min_num = computer+1
        else:
            print("电脑猜中咯咯哦哦哦哦哦")
            break

    print("电脑猜了%d次"%(i+1))
else:
    print("电脑内心:我不玩了")
发布了352 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/houlaos/article/details/104346025
今日推荐