- 帖子
- 14
- 精华
- 0
- 积分
- 103
- 阅读权限
- 20
- 注册时间
- 2019-11-5
- 最后登录
- 2020-1-15
|
from random import randint
name = input('请输入你的名字:')#输入玩家名字
f =open('D:\py\游戏.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 is None:
score =[0,0,0]
game_times =int(score[0])
min_times =int(score[1])
tatal_times =int(score[2])
if game_times > 0:
avg_times = float(tatal_times)/game_times
else:
avg_times =0
print(' %s 你已经玩了%d次,最少%d轮猜出答案,平均%.2f轮猜出答案' % (name,game_times, min_times, avg_times))
num=randint(0,100)
times = 0 #记录本次游戏轮数
print ('guess what i think?')
bingo=False
while bingo==False:
times +=1 #记录游戏轮数
answer=eval(input())
if answer>num:
print ('too big.')
if answer<num:
print ('too small')
if answer==num:
print ('bingo.')
bingo=True
f.close()
if game_times ==0 or times < min_times:
min_times = times
tatal_times +=times
game_times +=1
scores[name] = [str(game_times),str(min_times),str(tatal_times)]
result = ''
for n in scores:
line = n + ''+ ''.join(scores[n]) + '\n'
result +=line
f = open('D:\py\游戏.txt','w')
f.write(result)
f.close()
老师,能解释下为什么跑成这样么?
|
|