设为首页收藏本站

Crossin的编程教室

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

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

[复制链接]

0

主题

0

好友

8

积分

新手上路

Rank: 1

楼主
发表于 2015-10-5 14:20:26 |显示全部楼层
本帖最后由 zhangxw 于 2015-10-5 15:28 编辑
  1. from random import randint

  2. name = raw_input('Please input your name:')

  3. f = open('G:\PL\game.txt')
  4. lines=f.readlines()
  5. f.close()

  6. scores= {}
  7. for I in lines:
  8.     s = I.split()
  9.     scores[s[0]] = s[1:]
  10. score = scores.get(name)
  11. if score is None:
  12.     score = [0,0,0]

  13. game_times = int(score[0])
  14. min_times = int(score[1])
  15. total_times = int(score[2])

  16. if game_times>0:
  17.     avg_times = float(total_times)/game_times
  18. else:
  19.     avg_times = 0
  20. print '%s,you have played %d times,at least take %d times guess the answer,the \
  21. average time to guess the answer is %.2f'%(name,game_times,min_times,avg_times)
  22. #game process

  23. num = randint(1,100)
  24. times = 0
  25. print 'Guesss what I think?'
  26. bingo = False
  27. while bingo == False:
  28.     times += 1
  29.     answer = input()
  30.     if answer < num:
  31.         print 'Too small'
  32.     if answer > num:
  33.         print 'Too big'
  34.     if answer == num:
  35.         print 'bingo'
  36.         False = True

  37. if game_times == 0 or times < min_times:
  38.     min_times = times
  39. total_times += times
  40. game_times += 1

  41. scores[name] = [str[(game_times),str(min_times),str(total_times)]
  42. result = ''   #这一行报错 SyntaxError: invalid syntax 仔细看了  没错啊
  43. for n in scores:
  44.     line = n+''+''.join(scores[n])+'\n'
  45.     result += line
  46. f = open('G:\PL\game.txt','w')
  47. f.write(result)
  48. f.close()
复制代码
百思不得啊  明明没有错误  还是我有什么不知道的
python2.7
回复

使用道具 举报

0

主题

0

好友

8

积分

新手上路

Rank: 1

沙发
发表于 2015-10-7 15:58:32 |显示全部楼层
crossin先生 发表于 2015-10-6 11:11
scores[name] = [str[(game_times),str(min_times),str(total_times)]
这里括号对不上吧

居然是犯了这个错误  他一直报错下一行  我就没有检查上一行

看样子以后报错的那一行以及上面一行都要看看

多谢先生
回复

使用道具 举报

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

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

GMT+8, 2024-5-17 12:48 , Processed in 0.026272 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部