设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: mty1111
打印 上一主题 下一主题

【Python 第41课】 用文件保存游戏(3)

[复制链接]

4

主题

0

好友

34

积分

新手上路

Rank: 1

楼主
发表于 2017-12-14 16:54:49 |显示全部楼层
你好,想请教一个问题,代码如下:
from random import randint

name = raw_input('Plz print your Name:')
f = open('F:\py\game.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])
total_times = int(score[2])
if game_times > 0:
    avg_times = float(total_times) / game_times
else:
    avg_times = 0
print '%s, you have played %d times, the best takes %d times, avg takes %2f. times'%(name,game_times,min_times,avgtimes)

num = randint(1,100)
times = 0
print 'Guess what number i think?!'
bingo == False
while bingo==False:
    answer = input()
    if answer > num:
        print 'too small!'
    if answer < num:
        print 'too big!!'
    if answer == num:
        print 'BINGO!'
        bingo = True

if game_times == 0 or times < min_times:
    min_times = times
total_names += times
game_times += 1

socres[name]=[str(game_times),str(min_times),str(total_times)]
result = ''
for n in scores:
    line = n + ' ' + ' '.join(scores[n]) + '\n'
    result += line
    f = open('F:\py\game.txt','w')
    f.write(result)
    f.close()
结果总是报错:
File "C:\Users\asus\Desktop\L39-41 Game record by TXT 20171214.py", line 14, in <module>
    score = scores.get(name)
AttributeError: 'tuple' object has no attribute 'get'
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-5 12:11 , Processed in 0.027568 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部