- 帖子
- 5
- 精华
- 0
- 积分
- 16
- 阅读权限
- 10
- 注册时间
- 2018-1-27
- 最后登录
- 2018-1-28
|
from random import randint
name = raw_input('请输入你的名字:')
f = open('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 == None:
score = [0, 0 , 0, ]
game_times = int(score[0])
min_times = int(score[1])
total_times = int(score[2])
if game_times>0:
ave_times = float(total_times)/game_times
else:
ave_times = 0
print '%s已玩%d局,最快%d次猜出,平均每%.2f次猜出.'%(name,game_times,min_times,ave_times)
num = randint(1,100)
time = 0
bingo = False
print 'guess what i think?'
while bingo== False:
answer = int(input())
time +=1
if answer > num:
print 'too big!'
if answer <num:
print 'too small'
if answer ==num:
print 'bingo!'
bingo = True
if game_times == 0 or time < min_times:
min_times = time
total_times += time
game_times += 1
scores[name] = [str(game_times),str(min_times),str(total_times)]
result = ''
for n in scores:
line = n+ ' '+' '.jion(scores[n]) +'\n'
result += line
f = open('game.txt','w')
f.write(result)
f.close()
代码我基本上一个个对了一遍,,但是 每次运行都会出现:
请输入你的名字:张三
Traceback (most recent call last):
File "C:/Users/wangsheng/Desktop/py.py", line 13, in <module>
scores[s[0]]=s[1:]
IndexError: list index out of range
求教。。。。。
|
|