设为首页收藏本站

Crossin的编程教室

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

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

[复制链接]

1

主题

0

好友

23

积分

新手上路

Rank: 1

楼主
发表于 2016-4-12 17:33:40 |显示全部楼层
本帖最后由 fishpanchax 于 2016-4-12 17:37 编辑

请问为什么我点Run Module没有任何反应呢,连保存都没法保存

from random import randint

name = raw_input('请输入你的名字:')


f = file('game.txt')
lines = f.readlines()
f.close()

scores = {}
for l in lines:
   s = l.split()
   scores[s[0]] = s[1:]
score = scores.get(name)
if score is None:
   score = [0, 0, 0]

game_times = int(score[0])
min_times = int(score[1])
total_times = int(score[2])
if game_times > 0:
    avg_times = float(total_times)/game_times
else:
    avg_times = 0

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

num = randint(1,100)
times = 0
print('Guess what I think')
a = 0
while a== 0:
    answer = input('Your answer is:')
    times += 1
    if answer < num:
        print'too small'
    if answer > num:
        print'too large'
    if answer == num:
        print'BINGO!!!'
        a = 1

if game_times == 0 or times < min_times:
    min_times = times
total_times +=times
game_times += 1


scores[name] = [str(game_times), str(min_times), str(total_times)]
result = ''
for n in scores:
   line = n + ' ' + ' '.join(scores[n]) + '\n'
   result += line


f = file('game.txt','w')
f.write(result)
f.close()
回复

使用道具 举报

1

主题

0

好友

23

积分

新手上路

Rank: 1

沙发
发表于 2016-4-12 21:13:42 |显示全部楼层
fishpanchax 发表于 2016-4-12 17:33
请问为什么我点Run Module没有任何反应呢,连保存都没法保存

from random import randint

自己解决了,百度了一下,在前面加一行#coding=gbk
回复

使用道具 举报

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

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

GMT+8, 2024-6-15 05:19 , Processed in 0.024216 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部