设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 19350|回复: 0
打印 上一主题 下一主题

【Python 第39课】 用文件保存游戏(1)

[复制链接]

0

主题

0

好友

54

积分

注册会员

Rank: 2

楼主
发表于 2015-5-28 11:33:51 |显示全部楼层
  1. #coding:utf-8
  2. from random import randint

  3. #load file
  4. f=open('game.txt')
  5. score=f.read().split()

  6. game_times = int(score[0])
  7. min_times = int(score[1])
  8. total_times = int(score[2])

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

  16. num=randint(1,100)
  17. print "Guess what i think?"
  18. bingo = False
  19. while bingo == False:
  20.     answer = input()
  21.     if answer < num:
  22.         print "太小了!"
  23.     if answer > num:
  24.         print "太大了!"
  25.     if answer == num:
  26.         print "BINGO!"
  27.         bingo = True
复制代码
回复

使用道具 举报

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

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

GMT+8, 2024-5-3 11:27 , Processed in 0.029937 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部