设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 13403|回复: 0
打印 上一主题 下一主题

保存文件那章,,一直没弄懂我的问题在哪。。。。求教

[复制链接]

2

主题

0

好友

16

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2018-1-27 21:14:10 |只看该作者 |倒序浏览
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


求教。。。。。



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-11-23 10:30 , Processed in 0.022366 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部