- 帖子
- 25
- 精华
- 0
- 积分
- 72
- 阅读权限
- 20
- 注册时间
- 2018-4-5
- 最后登录
- 2018-10-21
|
- #读取文件中的成绩数据
- f=open('D:\game.txt')
- score=f.read().split()
- f.close()
- #分别存入变量中
- 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(game_times,min_times,avg_times)
- import random
- num=random.randint(1,5)
- guess=eval(input('guess what I think?'))
- #猜数过程
- def compare(x,y):
- if x<y:
- print('%d is too small' %x)
- elif x>y:
- print('%d is too big' %x)
- else:
- print('bingo')
- return x==y
- #开始猜数
- i=0
- while num!=guess:
- compare(guess,num)
- guess=eval(input('guess what I think?'))
- print('bingo')
-
复制代码 |
|