猜数字 AI Studio

百度AI Studio
学习打卡:python里面的三元运算与Java还是有区别的 记得input的类型不是int的

import random
import datetime
print("我们玩个游戏吧!一起来guess num!")
seed = 50
error_num = 1
while True:
    # input 输入类型默认为str
    x = int(input("请输入一个三位数字:"))
    str_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    first_num = int(str_time[(len(str_time))-2: len(str_time)])

    y = random.randrange(101, 1000)
    judge = "big" if x >= y else "small"
    guess_str = input("猜你输入的数比机器的数是big or small!猜对有奖励!big or small:")
    
    if guess_str == judge:
        print("you win!")
        print("you are good!")
        print("you are so boring!")
        print("You have the right idea!")
        break
        
    else:
     
        str_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        second_num = int(str_time[(len(str_time)) - 2:len(str_time)])
        think_time = (second_num - first_num) if second_num >= first_num else (first_num - second_num)
        
        print("正确答案是:{}".format(judge))

		print("You think for {} s.".format(think_time))
		
        print("Never give up! Come on! go on!")

系统随机猜你输入的数 ,完全看脸!
当然你可以手动设置猜数范围

import random
# 用户输入一个单位以内的数字,AI要用最少的次数猜中,并且显示出猜的次数和数字。
while True:
    y = int(input(" enter random: 0 ~ 1000 Please enter a number:"))
    
    if y in range(0, 1000):
    
        x = int(random.randrange(0, 1000))
        temp1 = 0
        temp2 = 0
        error_count = 0
        
        while True:
            error_count += 1
            if y == x :
                break
            if y > x :
                temp1 = x
                x = random.randrange(temp1, 1000)
            if y < x :
                temp2 = x
                x = random.randrange(temp1, temp2)
        print("Guess the {} time. The number of you entered is {}".format(error_count, x))
        
    else:
        print('请输入一个0 ~ 1000范围内的数!')

它会猜中你输入的任何数在范围 ( 2 63 + 1 ) (-2^{63}+1) ( 2 63 1 ) (2^{63}-1) 内的数 :

import random
x_max = random.randrange(0, (2**63-1))
x_min = random.randrange((-2**63+1), 0)
y = int(input("Please you enter a number:"))
print(x_max, x_min, y)
temp, count = 0, 0
while True:
    count += 1
    # 分析:
    # 1、y大于x_max
    if y > x_max:
        # 将值赋给x_min
        x_min = x_max
        # 将x_max的值在x_min~2**63-1范围内获得
        x_max = random.randrange(x_min, (2**63-1))
        print("1 x_max={} x_min={}".format(x_max, x_min))
    # 2、y小于x_max
    elif y < x_max:
        # 将x_min的值在x_min~x_max范围内获得
        temp = x_min
        x_min = random.randrange(x_min, x_max)
        if y < x_min:
            count += 1
            x_max = x_min
            x_min = temp
            print("2.1 x_max={} x_min={}".format(x_max, x_min))
    print("2 x_max={} x_min={}".format(x_max, x_min))
    if y == x_max:
        print("The computer guessed {} times. The number is {}".format(count, x_max))
        break
    if y == x_min:
        print("The computer guessed {} times. The number is {}".format(count, x_min))
        break

发布了18 篇原创文章 · 获赞 3 · 访问量 1063

猜你喜欢

转载自blog.csdn.net/hjh_cos/article/details/103827832
今日推荐