设为首页收藏本站

Crossin的编程教室

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

真搞不明白,文件中无数据,居然能读取出来

[复制链接]

3

主题

0

好友

41

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2017-1-25 11:26:28 |只看该作者 |倒序浏览
import random                  
RD = random.randint(1,100)      
f=open(r'C:\Python27\test\game.txt')#说下这个game.txt文件中最开始有默认值0 0 0 ,但是执行一次程序后,手动打开game文件是空白文档,
score=f.read().split()                           #在执行一次,却能成功运行,而且还能显示出数据,下面有个print score ,真不懂为啥
highest_times=int(score[0])
lowest_times=int(score[1])
current_times=int(score[2])
print score
f.close
print '''
请选择功能:
1.现在开始游戏
2.查看排行榜
3.退出
'''
you_id=input()
if you_id==1:
    i=0
    print "Guess what you think the number? And i will tips you big or samll."
    while True:
        you = input()
        if you>RD:
            print "%d is too big."%you
            i=i+1
        elif you<RD:
            print "%d is too small."%you
            i=i+1
        elif you==RD:
            i=i+1
            print "Bingo!Congratulations!%d is right!And you used %d chance."%(you,i)
            break;
    current_times=i
    if current_times>highest_times:
        highest_times=current_times
        score[0]=highest_times
    elif current_times<lowest_times:
        lowest_times=current_times
        score[1]=lowest_times
    score[2]=current_times
    score='%d %d %d'%(int(score[0]),int(score[1]),int(score[2]))
    print score
    f=open(r'C:\Python27\test\game.txt','w')
    f.write(score)
    f.close
elif you_id==2:
    print f.read()
    f.close()
else:
    exit()


回复

使用道具 举报

3

主题

0

好友

41

积分

新手上路

Rank: 1

沙发
发表于 2017-1-25 11:27:45 |只看该作者
论坛能@吗,@@@@@@@@@@@@crossin先生
回复

使用道具 举报

3

主题

0

好友

41

积分

新手上路

Rank: 1

板凳
发表于 2017-1-25 14:06:08 |只看该作者
握草,终于知道了,要等会在打开文件,写入的没这么快
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

地板
发表于 2017-1-26 18:38:12 |只看该作者
肖明 发表于 2017-1-25 14:06
握草,终于知道了,要等会在打开文件,写入的没这么快

f.close
后面没加括号,没有成功保存。并不是“写入没这么快”
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

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

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

GMT+8, 2024-4-20 09:04 , Processed in 0.025236 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部