- 帖子
- 2
- 精华
- 0
- 积分
- 8
- 阅读权限
- 10
- 注册时间
- 2018-11-8
- 最后登录
- 2018-11-9
|
本帖最后由 吃土小华 于 2018-11-9 15:18 编辑
吃土小华 发表于 2018-11-8 21:00
from random import randint
f = open('game.txt')
之前的问题已经解决了,可能是因为文本后面空白行的原因。
新的问题是这个
from random import randint
f = open('game.txt')
lines = f.readlines()
f.close()
scores = {}
for l in lines:
s = l.split()
scores[s[0]] = s[1:]
name = input('请输入你的名字:')
score = scores.get(name)
if score is None:
score = [0,0,0]
game_time = int(score[0])
min_time = int(score[1])
total_time = int(score[2])
if game_time>0:
avg_times = float(total_time)/game_time
else:
avg_times = 0
print('%s,你已经玩了%d次,最少%d轮猜出答案,平均%.2f轮猜出答案。'%(name,game_time,min_time,avg_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 small')
if answer>num:
print('too big')
if answer==num:
print('bingo')
bingo=True
total_time +=time
if game_time==0 or time<min_time:
min_time=time
game_time+=1
print(game_time,min_time,total_time)
game_times=str(game_time)
mins=str(min_time)
total=str(total_time)
scores[name] = {game_times,mins,total}
print(scores[name])
result=''
for n in scores:
line = n +' '+' '.join(scores[n])+'\n'#line为字符串
result += line
#f=open('game.txt','w')
#f.write(result)
#f.close()
这样写完后显示
请输入你的名字:小华
小华,你已经玩了0次,最少0轮猜出答案,平均0.00轮猜出答案。
Guess what I think.
50
too big
60
too big
25
too small
35
too small
40
too small
45
too small
48
too small
49
bingo
1 8 8
{'1', '8'}
>>>
其中小华的所有数值无法写入到字典里,只能显示两个值,文本文档中的内容和公众号一致,非常疑惑这是为什么,求大神解答,非常感谢。 |
|