Python3 实现猜数字小游戏

简介:利用随机函数生成0~999中的随机整数。当输入的数大于或者小于这个数,给出相应提示,猜对则结束!

import random
cmp = random.randint(0,999)
time = 1

small = 0
big = 999
answer = int(input("Input your answer(%d-%d): " %(small, big)))
while answer != cmp:
    time += 1
    if answer > cmp:
        print("Your answer is big")
        if answer > big:
            big = big
        else:
            big = answer
        answer = int(input("Input your answer(%d-%d): " %(small, big)))
    else:
        print("Your answer is small")
        if answer < small:
            small = small
        else:
            small = answer
        answer = int(input("Input your answer(%d-%d): " %(small, big)))
print("The answer is %d. You get it by %d times" %(cmp, time))
发布了84 篇原创文章 · 获赞 10 · 访问量 8575

猜你喜欢

转载自blog.csdn.net/AK47red/article/details/103772112