设为首页收藏本站

Crossin的编程教室

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

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

[复制链接]

0

主题

0

好友

88

积分

注册会员

Rank: 2

楼主
发表于 2018-3-13 15:48:21 |显示全部楼层
from random import randint
f=open('d:\Others\\text.txt')
score=f.read().split()
f.close()
cishu=int(score[0])
lunshu=int(score[1])
avg=int(score[2])
if cishu>0:
    avg=float(lunshu)/cishu
else:
    avg==0
print('游戏次数:%d,猜出所用的轮数:%d,平均每次所用轮数:%.2f'%(
    cishu,lunshu,avg))
num=randint(1,50)
print('猜一个1到50之间的数')
Biggo=False
time=0
while Biggo==False:
      time+=1
      answer=int(input())
      if answer>num:
          print('大了')
          Biggo==True
      if answer<num:
          print('小了')
          Biggo==True
      if answer==num:
          print('Biggo')
          Biggo==False
if cishu==0 or time<lunshu:
    lunshu==time
    cishu=cishu+1
    avg=float(lunshu)/cishu
data='%d,%d,%.2f'%(cishu,lunshu,avg)
f=open('d:\Others\\text.txt','w')
f.write(data)
f.clsoe()
能运行,但最后无法写入最新结果,自查半天也差不是原因,求教。。。
回复

使用道具 举报

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

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

GMT+8, 2024-5-7 15:13 , Processed in 0.024569 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部