- 帖子
- 10
- 精华
- 0
- 积分
- 34
- 阅读权限
- 10
- 注册时间
- 2017-12-14
- 最后登录
- 2018-7-26
|
TED 发表于 2017-12-14 17:07
scores = {} 这里是大括号,代表scores{}是个字典 不是小括号
多谢版主!昨天检查了出来,发现自己对字典的这个功能还不熟,修改了程序的几个小错误后,今天调试程序,然而发现一个小问题:游戏总次数是记录的,然而平均数和最好成绩始终是0,检查中... ....
print 'welcome to Guessing Game by Maxims Guo!'
from random import randint
name = raw_input('Plz print your Name:')
f = open('F:\py\game.txt')
lines = f.readlines()
f.close()
scores = {}
for l in lines:
s = l.split()
scores[s[0]] = s[1:]
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, you have played %d times, the best takes %d times, avg takes %.2f times'%(name, game_times, min_times, avg_times)
num = randint(1,50)
times = 0
print 'Guess what number i think?!'
bingo = False
while bingo==False:
answer = int(input())
if answer > num:
print 'too big!'
if answer < num:
print 'too small!'
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:
line = n + ' ' + ' '.join(scores[n]) + '\n'
result += line
f = open('F:\py\game.txt','w')
f.write(result)
f.close()
Plz print your Name:AA
AA, you have played 4 times, the best takes 0 times, avg takes 0.00 times
Guess what number i think?!
12
too small!
14
too small!
18
too small!
30
too big!
28
too big!
26
too small!
27
BINGO!
|
|