猜数字问题

# 题目:猜数字问题,要求如下:
# ① 随机生成一个整数
# ② 猜一个数字并输入
# ③ 判断是大是小,直到猜正确
# ④ 判断时间
# 提示:需要用time模块、random模块
# 该题目不需要创建函数

 

import time

import random

play_it = input('do you want to play it.(\'y\' or \'n\')') # 询问是否参与游戏
while play_it == 'y':
c = input('input a character:\n') # 输入参与游戏人物
i = random.randint(0,100)
print( 'please input number you guess:\n')
a = time.time() # 记录开始时间
guess = int(input('input your guess:\n'))
while guess != i:
if guess > i:
print( 'please input a little smaller')
guess = int(input('input your guess:\n'))
else:
print( 'please input a little bigger')
guess = int(input('input your guess:\n'))
b = time.time() # 记录结束时间
usedtime = b - a
print( 'It took you %.2f seconds' % usedtime)
if usedtime < 15:
print( 'you are very clever!')
elif usedtime < 25:
print( 'you are normal!')
else:
print( 'you are stupid!')
print( 'Congradulations')
print( 'The number you guess is %d' % i)
break
else:
print('hehe')

上一篇:python循环、条件判断


下一篇:leetcode解题之猜数字游戏