设为首页收藏本站

Crossin的编程教室

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

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

[复制链接]

0

主题

0

好友

88

积分

注册会员

Rank: 2

楼主
发表于 2018-3-14 16:03:01 |显示全部楼层
from random import randint
name=input('输入你的名字:')

f=open('d:\Others\\text.txt')
line=f.readlines()
f.close()

scores={}
for l in scores:
    s=l.split()    #s是每一列,带有每个玩家的信息(姓名,成绩)
    scores[s[0]]=s[1:]
score=scores.get(name)
if score is None:
    score=[0,0,0]

cishu=int(score[0])
lunshu=int(score[1])
zong=int(score[2])
if cishu>0:
    avg=float(zong)/cishu
else:
    avg=0
print('%s:你的成绩为:游戏次数:%d,猜出所用的轮数:%d,平均每次所用轮数:%.2f'%(
    name,cishu,lunshu,avg))
num=randint(1,50)
print('猜一个1到50之间的数:')
Bingo=False
time=0
while Bingo==False:
      time+=1
      answer=int(input())
      if answer>num:
          print('大了')
          Bingo==True
      if answer<num:
          print('小了')
          Bingo==True
      if answer==num:
          print('Bingo')
          Bingo==False
if cishu==0 or time<lunshu:
    lunshu=time
cishu=cishu+1
zong+=time
scores[name]=[str(cishu),str(lunshu),str(zong)]
result=''
for n in scores:
    line=n+' '+' '.join(scores[n])+'\n'
    result+=line
   
f=open('d:\Others\\text.txt','w')
f.write(result)
f.clsoe()
麻烦老师看下代码了,程序能运行,游戏也能玩,就是没法写入成绩,
自查不出来问题了。。。
回复

使用道具 举报

0

主题

0

好友

88

积分

注册会员

Rank: 2

沙发
发表于 2018-3-15 20:43:26 |显示全部楼层
额,犯了低级错误,添麻烦了,惭愧。。。
感谢,以后一定注意
回复

使用道具 举报

0

主题

0

好友

88

积分

注册会员

Rank: 2

板凳
发表于 2018-3-15 21:35:09 |显示全部楼层
crossin先生 发表于 2018-3-15 17:18
f.close()
最近好像好多人犯这个错啊……

先生啊,改了close,好像还是不行游戏还是能玩,就是写不进去结果
我用的py3,是有什么影响吗?

回复

使用道具 举报

0

主题

0

好友

88

积分

注册会员

Rank: 2

地板
发表于 2018-3-17 16:50:15 |显示全部楼层
用你说的方法,调试了半小时,终于弄清楚原因了:
while循环里,返回值bingo=True,不满足while条件了,while语句是不继续执行了,但程序也就停止了,while后面的程序都不执行了,必须在最后的if里(bingo=True)加个break,才能继续后面的程序。
可你的程序里没有break,其他都一样的,这是py3和2的差别?
回复

使用道具 举报

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

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

GMT+8, 2024-5-11 03:22 , Processed in 0.027284 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部