2021-04-27

python基础题目100例之九十一

  1. 猜数游戏
import time
import random

num = random.randint(0,100)
scope = [0,99]
guess = int(input("输入你猜的数字 \n当前范围是%d-%d:"%(scope[0],scope[1])))
start_time = time.time()
while True:
    if scope[0] > guess or guess > scope[1]:
        guess = int(input("超出范围! \n当前范围是%d-%d:"%(scope[0],scope[1])))
    elif guess > num:
        scope[1] = guess

        guess = int(input("猜大了,输入一个小的数字. \n当前范围是%d - %d :"%(scope[0],scope[1])))
    elif guess < num:
        scope[0] = guess

        guess = int(input("输入一个大的数字. \n当前的范围是%d - %d :"%(scope[0],scope[1])))
    else:
        end_time = time.time()
        print('猜对了,答案是 %d' % guess)
        break
count_time = end_time - start_time
print('一共耗时 %d 秒' % count_time)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值