- 帖子
- 9
- 精华
- 0
- 积分
- 33
- 阅读权限
- 10
- 注册时间
- 2018-1-19
- 最后登录
- 2018-1-25
|
老师好,
我在41课(用文件保存游戏3)里有几个问题:1.不管猜几次,文档里的记录次数总是比猜的次数多一次。 2. 文档里第二列(最少猜出答案轮数)和第三列(平均猜出答案轮数)总是0. 3. 保存游戏记录的文档是不是只能用txt?我试过用docx,结果不行。4. 代码第42行和第43行:if game_times==0 or times<min_times:
min_times=times
在第40课的解释是#如果是第一次玩,或者轮数比最小轮数少,则更新最小轮数。这句话是否这样理解:假如A是第一次玩,则文档自动记录一个新数据,若B是第3次玩,且用了比前两次更少的轮数猜出答案,则更新B的最少轮数?谢谢!
# -*- coding: utf-8 -*-
from random import randint
name=raw_input('your name please: ')
f=open('/Users/Max/Desktop/tires/game.txt')
lines=f.readlines()
f.close()
scores={} #inicialization de diccionario vacio
for l in lines:
s=l.split() #make each row of data as list
scores[s[0]]=s[1:] #first term is key, rest are values
score=scores.get(name) #search current player's data
if score is None:
score=[0, 0, 0]
game_times=int(score[0])
min_times=int(score[1])
total_times=int(score[2]) #depositar a cada variables
if game_times > 0:
avg_times=float(total_times)/game_times #calculo
else:
avg_times=0
print'you have played %d round, you got answer at least %d round,average round is %.2f'%(game_times, min_times, avg_times)
num=randint(1,10)
times=0
print'Guess number'
bingo=False
while bingo==False:
game_times +=1 #ronda mas 1
answer=input()
if answer<num:
print'too small'
if answer>num:
print'too big'
if answer==num:
print'bingo!'
bingo=True
if game_times==0 or times<min_times:
min_times=times # if it's first time or round is less than least round, then update the least round
total_times+=times #total round of game is increasing
game_times+=1 #game time is increasing
scores[name ]=[str(game_times), str(min_times), str(total_times)] #update score to determined player,str for reformation
result='' #inicializing empty string for data deposit purpose
for n in scores:
line=n+ ' ' + ' ' .join (scores[n])+'\n'
result+=line #agregando resultado
f=open('/Users/Max/Desktop/tires/game.txt','w')
f.write(result)
f.close()
-------------------------------------------------------
输出
your name please: Luo
you have played 0 round, you got answer at least 0 round,average round is 0.00
Guess number
6
bingo!
>>>
================= RESTART: /Users/Max/Desktop/tires/game.py =================
your name please: A
you have played 0 round, you got answer at least 0 round,average round is 0.00
Guess number
6
too small
8
too small
9
too small
10
bingo!
>>>
================= RESTART: /Users/Max/Desktop/tires/game.py =================
your name please: A
you have played 5 round, you got answer at least 0 round,average round is 0.00
Guess number
6
too small
9
too small
10
bingo!
>>>
------------------------------------------------------------
文档显示
A 9 0 0
Han 10 0 0
Luo 2 0 0
|
|