用Python完成猜随机数的游戏
2015-07-03 22:40
736 查看
from random import randint print '输入你的名字:' name = raw_input() f = open('game.txt') lines = f.readlines() scores = {}#初始化一个空字典 for l in lines: s = l.split() scores[s[0]] = s[1:] #print s[:]-----------------['yaoxiaokui', '1', '6', '6'] score = scores.get(name) if score is None: score = [0, 0, 0] game_times = int(score[0]) min_times = int(score[1]) total_times = int(score[2]) if game_times > 0: avg_times = float(total_times) / game_times else: avg_times = 0 print '%s,你已经玩了%d次,最少%d轮猜出答案,平均%.2f轮猜出答案' %(name, game_times, min_times,avg_times) print '输入数值的最大值' max = input() num = randint(1, max) times = 0#记录本次游戏轮数 print 'Guess what i think?' answer = input() while answer!=num: times += 1 if answer > num: print 'too large' if answer < num: print 'too small' answer = input() print 'You Win!' if game_times == 0 or times < min_times: min_times = times total_times += times game_times += 1 scores[name] = [str(game_times), str(min_times), str(total_times)] result = '' for n in scores: line = n + ' ' + ' '.join(scores ) + '\n' result += line f = file('game.txt', 'w') f.write(result) f.close()
相关文章推荐
- MAC OS上切换python2与python3环境
- python实践编程
- Python的os.walk()方法详细讲解
- Python笔记:_mysql常用操作
- python install pycurl
- Python笔记:MySQLdb模块常用操作
- Python - 函数/方法装饰器
- Python 自动化测试报告HTMLTestRunner
- Python 自动化测试报告HTMLTestRunner
- Python中logging.config配置文件解读
- Ubuntu下安装Python3.4
- python3.x __name__ == '__main__'
- python使用post方式发送json参数
- Python3.4字符串包含 & 集合
- Python正则表达式
- Python3.4-文本-替换字符串中的子串
- Python3.4字符串对齐/删除空格/合并/反转
- Python3.4字符串基础及每次处理一个字符
- Windows下Python添加库(模块)路径
- python检测是文件还是目录的方法