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

Crossin的编程教室

 找回密码
 立即加入
查看: 4930|回复: 8

求助:猜数游戏遇到的问题

[复制链接]

9

主题

0

好友

75

积分

注册会员

Rank: 2

发表于 2018-4-5 16:20:10 |显示全部楼层
本帖最后由 Ambition——xb 于 2018-4-5 17:05 编辑

麻烦大家跑一下,为什么只能记录一次数据,问题出在哪里?程序如下,非常感谢

print('Please input your name:')
name=input()

f=open('gameUp.txt')
lines=f.readlines()         #用readlines把每组成绩分开来:
f.close()
scores={}                   #初始化一个空字典
for I in lines:
    s=I.split()             #把每一行的数据拆分成list
    scores[s[0]]=s[1:]      #第一项作为key,剩下的作为value,source[key]=[value]

#字典类的get方法是按照给定key寻找对应项,如果不存在这样的key,就返回空值None。
score=scores.get(name)      #查找当前玩家的数据  变量name中存储的是字典的键  
if score is None:           #如果没找到
    score=[0, 0, 0]         #初始化数据

game_times=int(score[0])
min_times=int(score[1])
total_times=int(score[2])

from random import randint   #引入模块方法:from 模块名 import 方法名

times=0
num=randint(0,10)
print('Guess what I think?(0~10)')
begin=1
while begin==1:
    times+=1                #tiimes来记录每次游戏所用的轮数
    answer=int(input())
    if answer>num:
        print("It's too big")
    elif answer<num:
        print("It's too small")
    elif answer==num:
        print('BINGO! '+str(num)+' is the right answer!')
        begin=0

#如果是第一次玩,或者本次的轮数比最小轮数还少,就记录本次成绩为最小轮数
if game_times==0 or times<min_times:
    min_times=times
total_times+=times
game_times+=1
if game_times > 0:           #.因为0是不能作为除数的,所以这里还需要加上判断
   avg_times = float(total_times) / game_times

   """
   在total_times前加上了float,把它转成了浮点数类型再进行除法运算。
   如果不这样做,两个整数相除的结果会默认为整数,而且不是四舍五入。
   """
else:
   avg_times = 0
'''
把成绩更新到对应玩家的数据中,如果没有这一项,会自动生成新条目。
我们不能直接把这次成绩存到文件里,
那样就会覆盖掉别人的成绩。必须先把成绩更新到scores字典中,再统一写回文件中。

加str转成字符串,为后面的格式化作准备
'''
scores[name]=[str(game_times),str(min_times),str(total_times)]

#对于每一项成绩,我们要将其格式化:
result=''                              #初始化一个空字符串,用来存储数据
for n in scores:
    line=n+''+''.join(scores[n])+'\n'  #把成绩按照“name game_times min_times total_times”格式化
                                       #结尾要加上\n换行
    result +=line                      #添加到result中

print('%s,你已经玩了%d次,最少%d轮猜出答案,平均%.2f轮猜出答案'%(name,game_times,min_times,avg_times))

result='%d %d %d'%(game_times,min_times,total_times)
f=open('gameUp.txt','w')
f.write(result)
f.close()


回复

使用道具 举报

0

主题

0

好友

10

积分

新手上路

Rank: 1

发表于 2018-4-5 17:48:39 |显示全部楼层
你想达到的效果是什么?
回复

使用道具 举报

9

主题

0

好友

75

积分

注册会员

Rank: 2

发表于 2018-4-5 17:55:35 |显示全部楼层
本帖最后由 Ambition——xb 于 2018-4-5 17:56 编辑

记录下我玩了多少次,最快猜出来的轮数,以及平均每次猜对用的轮数。
并且存储多组成绩。玩家需要做的就是,在游戏开始前,输入自己的名字。然后记录数据
就是Crossin课程中该游戏的终极版  41课
回复

使用道具 举报

0

主题

0

好友

10

积分

新手上路

Rank: 1

发表于 2018-4-5 19:00:05 |显示全部楼层
Ambition——xb 发表于 2018-4-5 17:55
记录下我玩了多少次,最快猜出来的轮数,以及平均每次猜对用的轮数。
并且存储多组成绩。玩家需要做的就是 ...

我改了一下,我这里运行OK:
print('Please input your name:')
name=raw_input()

f=open('D:\Python 2.7\Logit file\gameUp.txt')
lines=f.readlines()         #用readlines把每组成绩分开来:
f.close()
scores={}                   #初始化一个空字典
for I in lines:
    s=I.split()             #把每一行的数据拆分成list
    scores[s[0]]=s[1:]      #第一项作为key,剩下的作为value,source[key]=[value]

#字典类的get方法是按照给定key寻找对应项,如果不存在这样的key,就返回空值None。
score=scores.get(name)      #查找当前玩家的数据  变量name中存储的是字典的键  
if score is None:           #如果没找到
    score=[0, 0, 0]         #初始化数据

game_times=int(score[0])
min_times=int(score[1])
total_times=int(score[2])

from random import randint   #引入模块方法:from 模块名 import 方法名

times=0
num=randint(0,10)
print('Guess what I think?(0~10)')
begin=1
while begin==1:
    times+=1                #tiimes来记录每次游戏所用的轮数
    answer=int(input())
    if answer>num:
        print("It's too big")
    elif answer<num:
        print("It's too small")
    elif answer==num:
        print('BINGO! '+str(num)+' is the right answer!')
        begin=0

#如果是第一次玩,或者本次的轮数比最小轮数还少,就记录本次成绩为最小轮数
if game_times==0 or times<min_times:
    min_times=times
total_times+=times
game_times+=1
if game_times > 0:           #.因为0是不能作为除数的,所以这里还需要加上判断
   avg_times = float(total_times) / game_times

   """
   在total_times前加上了float,把它转成了浮点数类型再进行除法运算。
   如果不这样做,两个整数相除的结果会默认为整数,而且不是四舍五入。
   """
else:
   avg_times = 0
'''
把成绩更新到对应玩家的数据中,如果没有这一项,会自动生成新条目。
我们不能直接把这次成绩存到文件里,
那样就会覆盖掉别人的成绩。必须先把成绩更新到scores字典中,再统一写回文件中。

加str转成字符串,为后面的格式化作准备
'''
scores[name]=[str(game_times),str(min_times),str(total_times)]

#对于每一项成绩,我们要将其格式化:
result=''                              #初始化一个空字符串,用来存储数据
for name in scores:
    line=name+' '+' '.join(scores[name])+'\n'  #把成绩按照“name game_times min_times total_times”格式化
                                       #结尾要加上\n换行
    result +=line                      #添加到result中

print('%s,你已经玩了%d次,最少%d轮猜出答案,平均%.2f轮猜出答案'%(name,game_times,min_times,avg_times))

#result='%s %d %d %d'%(name,game_times,min_times,total_times)
f=open('D:\Python 2.7\Logit file\gameUp.txt','w')
f.write(result)
f.close()
回复

使用道具 举报

9

主题

0

好友

75

积分

注册会员

Rank: 2

发表于 2018-4-5 22:54:57 |显示全部楼层
所以你改什么了,没看出来啊
回复

使用道具 举报

9

主题

0

好友

75

积分

注册会员

Rank: 2

发表于 2018-4-5 23:00:19 |显示全部楼层
luning3710642 发表于 2018-4-5 19:00
我改了一下,我这里运行OK:
print('Please input your name:')
name=raw_input()

所以你改啥了,没看出来啊这个程序本来就是能运行的,只是没有达到效果 比如
第一次输出:x 你已经玩了1次,最少3轮猜出答案,平均3.00轮猜出答案
第二次输出:x 你已经玩了2次,最少3轮猜出答案,平均3.50轮猜出答案
第三次。。。。。
换个人名,接着重新记录(不覆盖之前玩家的信息) 你跑改过的这个程序能实现吗?
回复

使用道具 举报

0

主题

0

好友

10

积分

新手上路

Rank: 1

发表于 2018-4-6 20:44:32 |显示全部楼层
Ambition——xb 发表于 2018-4-5 23:00
所以你改啥了,没看出来啊这个程序本来就是能运行的,只是没有达到效果 比如
第一次输出:x 你已经玩了1 ...

能实现, 亲测
回复

使用道具 举报

9

主题

0

好友

75

积分

注册会员

Rank: 2

发表于 2018-4-10 17:13:33 |显示全部楼层
luning3710642 发表于 2018-4-6 20:44
能实现, 亲测

还真是可以实现,请问你是改了哪了呀?没看出来啊
还有个小问题如下:
Please input your name:
xb
Guess what I think?(0~10)
5
It's too small
8
It's too small
9
BINGO! 9 is the right answer!
xb,你已经玩了1次,最少3轮猜出答案,平均3.00轮猜出答案
>>>
==== RESTART: D:\Learning_Software\Python\Documents\CrossinLesson\test.py ====
Please input your name:
XB
Guess what I think?(0~10)
4
It's too small
8
BINGO! 8 is the right answer!
XB,你已经玩了1次,最少2轮猜出答案,平均2.00轮猜出答案
>>>
==== RESTART: D:\Learning_Software\Python\Documents\CrossinLesson\test.py ====
Please input your name:
xb
Guess what I think?(0~10)
8
It's too big
5
BINGO! 5 is the right answer!
XB,你已经玩了2次,最少2轮猜出答案,平均2.50轮猜出答案

第三组换回人名xb后发现最后输出人名为XB
回复

使用道具 举报

0

主题

0

好友

6

积分

新手上路

Rank: 1

发表于 2018-4-13 11:13:10 |显示全部楼层
倒数第四行,result='%d %d %d'%(game_times,min_times,total_times)
你每次都result重新赋一行值然后保存到文件里
回复

使用道具 举报

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

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

GMT+8, 2024-3-29 15:57 , Processed in 0.025107 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部