标题: 新人求助!!关于第40课 用文件保存游戏我卡死在一个地方 [打印本页] 作者: 772546116 时间: 2019-3-7 21:19 标题: 新人求助!!关于第40课 用文件保存游戏我卡死在一个地方 程序如下所示
from random import randint
f=file('game.txt')
lines=f.readlines()
f.close()
scores={}
who=['']
i=0
for line in lines:
data=line.split()
scores[data[0]]=data[1:]
who[i]=data[0]#记录已参加游戏的玩家的姓名
i+=1
name=raw_input('请输入您的名字:')
for j in range(i):
if name==who[j]:
print '欢迎%s回到游戏'%name
score=scores.get(name)#查找当前玩家数据
break
else:
if j==i-1:
print'欢迎新玩家%s来到游戏'%name
score=[0,0,0]
else:
continue
game_times=int(score[0])
min_times=int(score[1])
total_times=int(score[2])
if game_times==0:
avg_times=0
else:
avg_times=float(total_times/game_times)
print'%s,你已经玩了%d次,最少%d轮猜出答案,平均%2.f轮猜出答案'%(name,game_times,min_times,avg_times)
def equal(num1,num2):
if num1<num2:
print'It is to small!'
bingo=False
elif num1>num2:
print'It is to big!'
bingo=False
elif num1==num2:
print'BINGO!'
bingo=True
s=1
while s==1:
num=randint(1,100)
print'Guess what I think?'
bingo=False
t=0
g=0
m=0
while bingo==False:
answer=input()
equal(answer,num)
t+=1
m+=1
print'是否想退出游戏,yes输入0,no输入1'
s=input()
g+=1
game_times+=g
total_times+=t
avg_times=float(total_times/game_times)
if m<min_times:
min_times=m
scores[name]=[str(game_times),str(min_times),str(total_times)]
result=''
for n in scores:
line=n+''+''.join(scores[n])+'\n'
result+=line
output=file('game.txt','w')
f.write(game)
f.close()
我一直卡死的地方在while s==1那个循环里面 部分结果截图如下:
请输入您的名字:zhaoxu
欢迎新玩家zhaoxu来到游戏
zhaoxu,你已经玩了0次,最少0轮猜出答案,平均 0轮猜出答案
Guess what I think?
50
It is to big!
40
It is to big!
30
It is to big!
20
BINGO!
20
BINGO!
20
BINGO!
20
BINGO!