Python就业班——Python函数与模块——练习——number_riddle猜数字(慕K)
#!/usr/bin/env python3 # coding=utf-8 # Version:python3.6.1 # Project:practice # File:Number_Riddle_猜数字.py # Data:2020/7/2 21:36 # Author:LGSP_Harold import random from datetime import datetime def func(m, n): random_region = random.randint(m, n) j = 0 file_name = 'number_riddle.log' f = open(file_name, 'a', encoding='utf-8') while True: i = input('请输入猜测的数字:') if not i.isdigit(): print('请输入数字') i = int(i) j += 1 if i < m or i > n: print('数字不在区间内') elif i < random_region: print("小了") result = '{0}次,小了:{1}\n'.format(j, datetime.now()) f.write(result) elif i > random_region: print("大了") result = '{0}次,大了:{1}\n'.format(j, datetime.now()) f.write(result) else: print('恭喜,用了{}次'.format(j)) result = '恭喜,用了{0}次,当前时间:{1}\n'.format(j, datetime.now()) f.write(result) break f.close() def number_riddle(): print('进入小游戏') m = input('数字区间起始值:') n = input('数字区间终止值:') if not m.isdigit() or not n.isdigit(): print('输入的不是数字') elif m >= n: print('输入区间大小有误') else: m = int(m) n = int(n) print('所产生的随机数字区间为:["{0}", "{1}"]'.format(m, n)) func(m, n) if __name__ == '__main__': number_riddle()
略懂,略懂....