设为首页收藏本站

Crossin的编程教室

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

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

[复制链接]

0

主题

0

好友

96

积分

注册会员

Rank: 2

楼主
发表于 2016-12-14 14:53:52 |显示全部楼层
  1. #-*-coding:utf-8-*-
  2. a = 1
  3. while a == 1:
  4.     from random import randint
  5.     f = open('jifen.txt')
  6.     score = f.read().split()
  7.     game_times = int(score[0])#总游戏数
  8.     min_times = int(score[1])#最少轮数
  9.     total_times = int(score[2])#总轮数
  10.     if game_times > 0:
  11.         avg_times = float(total_times)/game_times
  12.     else:
  13.         avg_times = 0
  14.     print ' 你已经玩%d了次,最少%d轮猜出答案,平均%.2f轮猜出答案 '%(game_times,min_times,avg_times)

  15.     num = randint(1 , 100)
  16.     times = 0
  17.     print ' 请猜一个100以内的数字'
  18.     bingo = False
  19.     while bingo == False:
  20.         times += 1
  21.         answer = input()
  22.         if answer < num:
  23.             print 'too small'
  24.         if answer > num:
  25.             print 'too big'
  26.         if answer == num:
  27.             print 'bingo'
  28.             bingo = True
  29.    
  30.     if game_times == 0 or times < min_times:
  31.         min_times = times
  32.     total_times += times
  33.    
  34.     game_times += 1

  35.     jilu = '%d %d %d'%(game_times,min_times,total_times)
  36.     f = open('jifen.txt', 'w')
  37.     f.write(jilu)
  38.     f.close()
复制代码
回复

使用道具 举报

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

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

GMT+8, 2024-5-3 14:44 , Processed in 0.025472 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部