大家好,想请教一个问题,代码如下: 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' |
TED 发表于 2017-12-15 13:36
min_times = int(score[1])-------------------------------------------一开始就定义了min是0
。。。。。 ...
欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) | Powered by Discuz! X2.5 |