猜字游戏
1、用到的知识点:
1)input(),键盘输入字符
2)random.randint()随机生成一个数字,用于做答案
3)while循环,一定有跳出循环的语句
4)if...else判断语句
5)time.time()记录当前时间,用于做时长统计
6)round() 取四舍五入的值
2、上代码
import random import time #准备 while True: start = input('The game will start,are you ready? y/n\n') if start != 'y': print('please input again') else: print('OK,It begin,It is a number between 0 and 999') break #随机设置一个数字 n = random.randint(0,1000) #开始计时 starttime = time.time() #猜字 while True: num = int(input('input your number:')) if num < n: print ('your number is smaller') elif num > n: print ('your number is bigger') else: print('congradulations,you are right') break #结束计时 endtime = time.time() #计算时长,四舍五入取整 durtime = round(endtime-starttime) #针对时长,做出判断 if durtime < 30: print('you used %d s,A' %durtime) elif durtime < 60: print('you used {}s,B'.format(durtime)) else: print('you used {}s,C'.format(durtime))