- 帖子
- 21
- 精华
- 0
- 积分
- 96
- 阅读权限
- 20
- 注册时间
- 2016-12-2
- 最后登录
- 2017-2-13
|
- #-*-coding:utf-8-*-
- a = 1
- while a == 1:
- from random import randint
- name = raw_input('请输入你的名字')#输入名字
-
- f = open('jifen.txt')
- lines = f.readlines()#读取文件内容并制成list
- f.close()
- scores = {}#建立一个字典
- for i in lines:#遍历lines
- s = i.split()#通过空格隔开制成list
- scores[s[0]] = s[1:]#从list提出元素并在字典里赋值,名字是KEY,数据是VALUE
- 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 ' 你已经玩%d了次,最少%d轮猜出答案,平均%.2f轮猜出答案 '%(game_times,min_times,avg_times)
- num = randint(1 , 100)
- times = 0
- print ' 请猜一个100以内的数字'
- bingo = False
- while bingo == False:
- times += 1
- answer = input()
- if answer < num:
- print 'too small'
- if answer > num:
- print 'too big'
- if answer == num:
- print 'bingo'
- bingo = True
-
- 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:#遍历scores的KEY
- line = n + ' ' +' '.join(scores[n]) + '\n'
- #n是scores中的key也就是名字 scores[n] 是value 也就是数据
- result += line#将遍历出来的元素加进result字符组
- f = open ('jifen.txt','w')
- f.write(result)
- f.close
-
复制代码 老师,请帮我看下后面的注解对不对,想了好长时间 |
|