Crossin的编程教室

标题: Py入门 第【41课】代码问题 [打印本页]

作者: ouyezhe886    时间: 2017-12-14 16:56
标题: Py入门 第【41课】代码问题
大家好,想请教一个问题,代码如下:
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-14 17:07
scores = {} 这里是大括号,代表scores{}是个字典 不是小括号
作者: ouyezhe886    时间: 2017-12-15 11:29
TED 发表于 2017-12-14 17:07
scores = {} 这里是大括号,代表scores{}是个字典 不是小括号

多谢版主!昨天检查了出来,发现自己对字典的这个功能还不熟,修改了程序的几个小错误后,今天调试程序,然而发现一个小问题:游戏总次数是记录的,然而平均数和最好成绩始终是0,检查中... ....
print 'welcome to Guessing Game by Maxims Guo!'

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, avg_times)

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

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

scores[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()
Plz print your Name:AA
AA, you have played 4 times, the best takes 0 times, avg takes 0.00 times
Guess what number i think?!
12
too small!
14
too small!
18
too small!
30
too big!
28
too big!
26
too small!
27
BINGO!

作者: 散不开暧昧    时间: 2017-12-15 12:11
ouyezhe886 发表于 2017-12-15 11:29
多谢版主!昨天检查了出来,发现自己对字典的这个功能还不熟,修改了程序的几个小错误后,今天调试程序, ...

while 循环里面 开始少一句 times+=1
作者: ouyezhe886    时间: 2017-12-15 12:21
散不开暧昧 发表于 2017-12-15 12:11
while 循环里面 开始少一句 times+=1

Plz print your Name:AA
AA, you have played 9 times, he best takes 0 times, avg takes 2.33 times
Guess what number i think?!

多谢!! avg_times是解决了,但是min_times还是0 TT
作者: ouyezhe886    时间: 2017-12-15 12:23
散不开暧昧 发表于 2017-12-15 12:11
while 循环里面 开始少一句 times+=1

我感觉是少了一个记录min_times的小步骤,导致min_times一直是0,所以times>min_times一直成立,所以min_times一直也是0
作者: TED    时间: 2017-12-15 13:36
min_times = int(score[1])-------------------------------------------一开始就定义了min是0

。。。。。--------------------------在这中间加点东西就行了
if game_times == 0 or times < min_times:---------------------这一步之前得把min_times设置个比较大的值才能实现
    min_times = times
作者: ouyezhe886    时间: 2017-12-15 14:18
TED 发表于 2017-12-15 13:36
min_times = int(score[1])-------------------------------------------一开始就定义了min是0

。。。。。 ...

解决了! 看来细心点,多思考才能解决问题! 多谢TED和暧昧两位版主




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5