Python 练习实例94

[复制链接]
641|0
 楼主| cooldog123pp 发表于 2023-8-20 16:07 | 显示全部楼层 |阅读模式
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。
程序分析:无。
程序源代码:
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-

  3. if __name__ == '__main__':
  4.     import time
  5.     import random
  6.    
  7.     play_it = raw_input('do you want to play it.(\'y\' or \'n\')')
  8.     while play_it == 'y':
  9.         c = raw_input('input a character:\n')
  10.         i = random.randint(0,2**32) % 100
  11.         print 'please input number you guess:\n'
  12.         start = time.clock()
  13.         a = time.time()
  14.         guess = int(raw_input('input your guess:\n'))
  15.         while guess != i:
  16.             if guess > i:
  17.                 print 'please input a little smaller'
  18.                 guess = int(raw_input('input your guess:\n'))
  19.             else:
  20.                 print 'please input a little bigger'
  21.                 guess = int(raw_input('input your guess:\n'))
  22.         end = time.clock()
  23.         b = time.time()
  24.         var = (end - start) / 18.2
  25.         print var
  26.         # print 'It took you %6.3 seconds' % time.difftime(b,a))
  27.         if var < 15:
  28.             print 'you are very clever!'
  29.         elif var < 25:
  30.             print 'you are normal!'
  31.         else:
  32.             print 'you are stupid!'
  33.         print 'Congradulations'
  34.         print 'The number you guess is %d' % i
  35.         play_it = raw_input('do you want to play it.')
以上实例输出结果为:
  1. do you want to play it.('y' or 'n')y
  2. input a character:
  3. 5
  4. please input number you guess:

  5. input your guess:
  6. 60
  7. ……
  8. please input a little bigger
  9. input your guess:
  10. 29
  11. please input a little smaller
  12. input your guess:
  13. 28
  14. 3.81868131868e-05
  15. you are very clever!
  16. Congradulations
  17. The number you guess is 28
  18. do you want to play it.


您需要登录后才可以回帖 登录 | 注册

本版积分规则

2304

主题

7628

帖子

31

粉丝
快速回复 在线客服 返回列表 返回顶部