请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: mty1111

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

[复制链接]

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2017-11-4 15:28:22 |显示全部楼层
lubvi 发表于 2017-11-3 17:16
C老师,为啥第一行的0 0 0 不会被替换掉啊,之后生成新的

你把中间产生的变量都输出出来自己看一下就明白了
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

8

积分

新手上路

Rank: 1

发表于 2017-11-20 08:52:54 |显示全部楼层

请教老师,这是哪里出问题了

name=input('请输入你的名字:')
f1=open('d:\py\game.txt')
lines=f1.readlines()   #读取文件。与之前不同,我们用readlines把每组成绩分开来
f1.close()

scores={}              #再用一个字典来记录所有的成绩
for i in lines:
    s=i.split()
    scores[s[0]]=s[1:] #每一项的key是玩家的名字,value是一个由剩下的数据组成的数组。把成绩赋值给键
score=scores.get(name) #查找当前玩家数据
if score is None: #没有找到该玩家数据,说明是新玩家,给他初始化一组成绩:
    socre=[0,0,0]

from random import randint #用random模块中的randint这个方法产生随机数
game_times=int(score[0])  #玩游戏次数
min_times=int(score[1])   #最快猜出的轮数
total_times=int(score[2]) #猜过的总轮数
if game_times>0:         #分母不能为0,所以加了个判断
    avg_times=float(total_times)/game_times  #平均数可能是小数,所以在之前加个float
else:
    avg_times=0
print('%s,你已经玩了%d次,最少%d轮猜出答案,平均%.2f轮猜出答案' %(
    name,game_times,min_times,avg_times))

num=randint(1,100)
times=0            #记录本次游戏轮数
print('gusee what i think?')
bingo=False  #先让循环动起来!
while bingo==False:
    times+=1       #每进行一次就记录一次轮数
    answer=int(input())
    if answer<num:
        print('%.2f is too small!' %answer)
    if answer>num:
        print('%.2f is too big!'%answer)
    if answer<0:
        print('exit game')
        break             #输入负数提前结束游戏
    if answer==num:
        print('bingo,%.2f is the right answer!'%answer)
        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)] #把成绩更新到scores中,如果没有这一项,会自动生成新条目
result=''    #初始化一个空字符串
for n in scores:
    line=n+''+' '.join(scores[n])+'\n' #把成绩按照“game_times,min_times,total_times”格式化
    result+=line  #添加到result中

f1=open('d:\py\game.txt','w')
f1.write(result)    #把结果输入到文件中
f1.close()
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2017-11-20 10:06:12 |显示全部楼层
bobo0769 发表于 2017-11-20 08:52
name=input('请输入你的名字:')
f1=open('d:\py\game.txt')
lines=f1.readlines()   #读取文件。与之前不 ...

请提供输出或报错,你不能光把代码放上来让别人来调试
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

4

主题

0

好友

98

积分

注册会员

Rank: 2

发表于 2017-11-30 16:17:15 |显示全部楼层
Envy 发表于 2015-3-25 16:30
你好,那个scores和score这块不太明白是什么意思,就是readlines得到的应该是一个list吧,如果最开始game ...

明白了,这个按行读取会分别按行保存,然后循环从第二个数据读取,原来如此,谢谢啦
回复

使用道具 举报

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'
回复

使用道具 举报

0

主题

0

好友

34

积分

新手上路

Rank: 1

发表于 2017-12-21 22:50:47 |显示全部楼层
   line=n+' '+' '.join(scores[n])+'\n'
这个不是特别理解,n是指的名字,我不太理解为什么是两个空格,是因为n是名字,名字与数字间的空格需要加一个空格强调一下,后面的都是数字了就可以只有一个空格吗?
编程新人,求大神解答
  
回复

使用道具 举报

2

主题

0

好友

60

积分

注册会员

Rank: 2

发表于 2018-2-9 15:34:50 |显示全部楼层
老师能解释一下这句吗
for n in scores:
     line=n+' '+' '.join(scores[n])+'\n'
     result+=line
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2018-2-9 16:23:49 |显示全部楼层
wwyy4ever 发表于 2018-2-9 15:34
老师能解释一下这句吗
for n in scores:
     line=n+' '+' '.join(scores[n])+'\n'

这个就是拼字符串,你把它分开来一步步写会比较清楚,我这里合在一行里了
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

10

积分

新手上路

Rank: 1

发表于 2018-2-18 17:34:28 |显示全部楼层
老师,过年好,我是新手
scores={}
for i in lines:
    #print(i)
    s=i.split()
    print(s)
    scores[s[0]]=s[1:]
    print(scores)
score = scores.get(name)
print(score)
if name is None:
    score=[0,0,0]
game_times=int(score[0])
最后输出有错误
输入名字a
['0', '0', '0']
{'0': ['0', '0']}
None
Traceback (most recent call last):
  File "e:\vscod\a8.py", line 18, in <module>
    game_times=int(score[0])
TypeError: 'NoneType' object is not subscriptable
通过退出代码 1 终止的终端进程
老师,为何新名字添加不进去,txt文件是0 0 0,名字为空,程序就停了,
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2018-2-19 10:42:05 |显示全部楼层
t111222333 发表于 2018-2-18 17:34
老师,过年好,我是新手
scores={}
for i in lines:

代码里有空行
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

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

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

GMT+8, 2024-3-29 14:36 , Processed in 0.026183 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部