|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
题目:时间函数举例4。
! r1 S/ x& r7 T程序分析:如何浪费时间7 O% P4 m" f2 T
- if __name__ == '__main__':* X6 C: s' G; p* u9 q- [8 P. s5 X. I
- import time
# _& E. t1 P9 q3 w - import random
# G: \% N9 U6 s, O, x - 7 ?0 P7 Y; P$ X$ `- Y% @' b
- play_it = input('do you want to play it.(\'y\' or \'n\')')
, o* N4 S, b" h3 _4 M6 s - while play_it == 'y':
( O& k. L# M+ T+ e* ~ - c = input('input a character:\n')! D2 ^0 ?& ]& h/ U# X0 b
- i = random.randint(0,2**32) % 100
6 u5 v/ A; v( x& {) P9 y - print ('please input number you guess:\n')2 s7 I2 Q# Q. Z% ?7 M' |
- start = time.clock()
$ o" t( h; {! K - a = time.time() A5 |! B0 `" L- H/ j; M
- guess = int(input('input your guess:\n'))
5 X) a" {' Q: T/ [5 g& { - while guess != i:
- ]( A5 t, C: }+ p3 U - if guess > i:; l" p/ g& j6 y: T
- print('please input a little smaller')
+ D8 W: ]" E% b- x [3 i, N* Y - guess = int(input('input your guess:\n'))
T$ p8 G( n$ A$ C0 s& p) g - else:
0 ^' L% s; L; Z i6 S - print('please input a little bigger')
0 ?! ?2 K$ b4 V" s - guess = int(input('input your guess:\n'))8 w" C3 v: |9 _( L7 W- U3 G
- end = time.clock()9 B6 K) ~2 z* k: ^' E( l: t
- b = time.time()
5 R+ h7 \- v" z5 _6 i' j/ Y - var = (end - start) / 18.2
1 x8 _; }# b1 ^/ l9 v1 M2 m% J1 w2 `2 p - print (var)3 Y; z* R- M( o+ @& x4 S' j
- # print 'It took you %6.3 seconds' % time.difftime(b,a))6 F# `# x, j6 U9 Q5 i8 E) ^& i
- if var < 15:+ M9 C/ X' y1 Y& z- M" C6 S
- print ('you are very clever!')
# v5 H$ h: X6 I9 q8 T9 u. h5 [ - elif var < 25:5 I1 i" Y2 i$ E0 S
- print ('you are normal!')
; X) `; e8 O# Q - else:
: y/ q8 b: ]; X1 D2 {; C - print ('you are stupid!')
Y! e) _' J: b& K* `# c( v; n - print ('Congradulations')
$ h- n% c" N. Q7 y9 c - print ('The number you guess is %d' % i)
, n' K# b& w/ F& [# Q% Z2 e3 r' k - play_it = input('do you want to play it.')
复制代码 |
|