- 帖子
- 6
- 精华
- 0
- 积分
- 18
- 阅读权限
- 10
- 注册时间
- 2020-9-13
- 最后登录
- 2020-9-14
|
crossin先生 发表于 2020-9-14 17:56
d.close()
少了括号,可能是这个原因
大神,你好,我加上括号后,但是每次玩得数据,程序都会在下一行保存,不会再原来的数据上进行添加,是哪里出错了啊
def isEqual(num1,num2):
if num1<num2:
print('too small')
return False
elif num1>num2:
print('too big')
return False
else:
print('bingo')
return True
from random import randint
name=input('输入你的名字:')
f=open('game.txt','r',encoding='gbk')
lines=f.readlines()
f.close()
print(lines)#查看文本数据
scores={}#初始化一个空字典
for l in lines:
s=l.split()#把每一行的数据拆分成list
scores[s[0]]=s[1:]#第一项作为Key剩下的作为value
score=scores.get(name)#找到当前玩家的数据
if score is None:
score=[0,0,0]
print(score)#查看玩家数据
game_times=int(score[0])
min_times=int(score[1])
total_times=int(score[2])
if game_times>0:
avg_times=total_times/game_times
else:
avg_times=0
print('%s,你已经玩了%d次,最少%d轮猜出答案,平均%.2f轮猜出答案'%(name,game_times,min_times,avg_times))
num=randint(1,10)
times=0
print('Guess what I think')
bingo=False
while bingo==False:
times+=1
answer=int(input())
bingo=isEqual(answer,num)
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
print(result)
d=open('game.txt','w',encoding='gbk')
d.write(result)
d.close()
#查看数据是否保存
f=open('game.txt',encoding='gbk')
line=f.readlines()
f.close()
print(line)
|
|