设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 9847|回复: 7
打印 上一主题 下一主题

Py入门 第【41课】代码问题

[复制链接]

4

主题

0

好友

34

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2017-12-14 16:56:42 |只看该作者 |倒序浏览
大家好,想请教一个问题,代码如下:
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'

回复

使用道具 举报

6

主题

2

好友

654

积分

实习版主

Rank: 7Rank: 7Rank: 7

沙发
发表于 2017-12-14 17:07:11 |只看该作者
scores = {} 这里是大括号,代表scores{}是个字典 不是小括号
回复

使用道具 举报

4

主题

0

好友

34

积分

新手上路

Rank: 1

板凳
发表于 2017-12-15 11:29:46 |只看该作者
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!
回复

使用道具 举报

1

主题

0

好友

55

积分

实习版主

Rank: 7Rank: 7Rank: 7

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

while 循环里面 开始少一句 times+=1
回复

使用道具 举报

4

主题

0

好友

34

积分

新手上路

Rank: 1

5#
发表于 2017-12-15 12:21:31 |只看该作者
散不开暧昧 发表于 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
回复

使用道具 举报

4

主题

0

好友

34

积分

新手上路

Rank: 1

6#
发表于 2017-12-15 12:23:11 |只看该作者
散不开暧昧 发表于 2017-12-15 12:11
while 循环里面 开始少一句 times+=1

我感觉是少了一个记录min_times的小步骤,导致min_times一直是0,所以times>min_times一直成立,所以min_times一直也是0
回复

使用道具 举报

6

主题

2

好友

654

积分

实习版主

Rank: 7Rank: 7Rank: 7

7#
发表于 2017-12-15 13:36:37 |只看该作者
min_times = int(score[1])-------------------------------------------一开始就定义了min是0

。。。。。--------------------------在这中间加点东西就行了
if game_times == 0 or times < min_times:---------------------这一步之前得把min_times设置个比较大的值才能实现
    min_times = times
回复

使用道具 举报

4

主题

0

好友

34

积分

新手上路

Rank: 1

8#
发表于 2017-12-15 14:18:29 |只看该作者
TED 发表于 2017-12-15 13:36
min_times = int(score[1])-------------------------------------------一开始就定义了min是0

。。。。。 ...

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

使用道具 举报

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

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

GMT+8, 2024-5-10 15:36 , Processed in 0.027969 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部