设为首页收藏本站

Crossin的编程教室

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

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

[复制链接]

0

主题

0

好友

18

积分

新手上路

Rank: 1

楼主
发表于 2018-3-19 14:52:49 |显示全部楼层
老师你好
运行代码提示
Traceback (most recent call last):
  File "C:/Users/chenh/PycharmProjects/untitled/1.py", line 9, in <module>
    game_times=int(score[0])
ValueError: invalid literal for int() with base 10: '\xef\xbb\xbf0'

这个报错一直出现,弄得摸不着头脑。
谢谢指教!
回复

使用道具 举报

0

主题

0

好友

18

积分

新手上路

Rank: 1

沙发
发表于 2018-3-19 14:53:09 |显示全部楼层
#coding:utf-8
from random import randint

#load file
f=open('C:\\Users\chenh\Desktop\game.txt')
score=f.read().split()
f.close()
game_times=int(score[0])
min_times=int(score[1])
total_times=int(score[2])

#judge
if game_times>0:
    avg_times = float(total_times)/game_times
else:
    avg_times = 0
#output result
print "你已经玩了%d次,最少%d轮猜出了答案,平均%.2f轮猜出了答案"%(game_times,min_times,avg_times)

num=randint(1,100)
times = 0
print "Guess what i think?"
bingo = False
while bingo == False:
    times+=1
    answer = input()
    if answer < num:
        print "太小了!"
    if answer > num:
        print "太大了!"
    if answer == num:
        print "BINGO!"
        bingo = True
if game_times ==0 or times <= min_times:
    min_times = times
total_times +=times
game_times +=1
result = '%d %d %d'%(game_times,min_times,total_times)
f = open('C:\Users\chenh\Desktop\game.txt','w')
f.write(result)
f.close()
回复

使用道具 举报

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

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

GMT+8, 2024-5-7 17:38 , Processed in 0.030005 second(s), 24 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部