设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: mty1111
打印 上一主题 下一主题

【Python 第40课】 用文件保存游戏(2)

[复制链接]

0

主题

0

好友

56

积分

注册会员

Rank: 2

楼主
发表于 2017-6-22 17:24:50 |显示全部楼层

老师,程序可以运行,但是无法写入成绩,肿么回事啊@Crossin

本帖最后由 jinss 于 2017-6-23 15:19 编辑

f=file("E:\python\pycharm\python\game.txt")
score=f.read().split()
f.close()
game_times=int(score[0])
min_times=int(score[1])
total_times=int(score[2])
if game_times>0:
    ave_times=float(total_times)/game_times
else:
    ave_times=0
print'you have played %d round, at least is %d try  ,avg is %.2f'%(game_times,min_times,ave_times)
from random import randint
times=0
print'guess what I think?'
num=randint(0,100)
bingo=False
while bingo==False:
    times+=1
    num1 = input()
    if num1>num:
        print'too big'
    if num1<num:
        print"too small"
    if num1==num:
        print'bingo'
        bingo=True
if game_times==0 or times<min_times:
    min_times=times
total_times+=times
game_times=game_times+1
result='%d,%d,%.2f'%(game_times,min_times,total_times)
f=file('E:\python\pycharm\python\game.txt','w')
f.write(result)
f.close()
回复

使用道具 举报

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

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

GMT+8, 2024-5-7 20:41 , Processed in 0.038206 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部