- 帖子
- 4
- 精华
- 0
- 积分
- 26
- 阅读权限
- 10
- 注册时间
- 2018-5-31
- 最后登录
- 2018-6-14
|
python 3.5 帮忙指导下,报错:
Traceback (most recent call last):
File "E:\workspace\learn\src\less01\game_tst1.py", line 63, in <module>
line=n+''+''.join(score[n])+'\n'
TypeError: list indices must be integers or slices, not str- #coding=utf-8
- from random import randint
- from pip._vendor.distlib.compat import raw_input
- name=raw_input("请输入你的名字")
- f=open(r'e:\test\game.txt','r')
- lines=f.readlines()
- 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,你已经完料%d次,最少%d轮猜出答案,平均%.2f轮拆除答案"%(name,game_times,min_times,avg_times))
- times=0
- num=randint(1,100)
- print("Guess what i think?")
- bingo=False
- while bingo==False:
- times=times+1
- answer=int(input())
- if answer<num:
- print(("too small"))
- if answer>num:
- print("too big")
- if answer==num:
- print("you are clevr!")
- bingo=True
-
- if game_times==0 or times<min_times:
- min_times=times
- total_times=total_times+times
- game_times=game_times+1
- scores[name]=[str(game_times),str(min_times),str(total_times)]
- result=''
- for n in scores:
- line=n+''+''.join(score[n])+'\n'
- f=open(r'e:\test\game.txt','w')
- f.write(result)
- f.close()
复制代码 |
|