|

楼主 |
发表于 2006-8-15 18:03:11
|
显示全部楼层
Post by Archetype
看一下time模块的time()函数。
恩,的确如此,看来还是对函数不够熟悉
Archetype兄,玩玩这个小游戏吧,没有你的帮助这个小游戏肯定完成不了。
实在是很寒颤的代码,各位不要嘲笑我这个菜鸟啊
- #!/usr/bin/python
- #Filename: rand-0.1.1.py
- #Writer:Frank(sanfanling) Lisence:GPL
- #changelog:add all time "clear" function;re-design the output
- import random,os,time,re
- os.system('clear')
- while True:
- path_home=os.getenv('HOME')
- if(os.access(path_home+'/.randata.log',os.F_OK)):
- pass
- else:
- os.system('touch $HOME/.randata.log')
- print '''1 Play game
- 2 See the best record
- 3 Delete the log file
- 4 Quit'''
- ask0=raw_input('please select:')
- if(ask0=='1'):
- os.system('clear')
- file=open(path_home+'/.randata.log','a')
- ask1=''
- while ask1=='':
- time1=time.time()
- seed=random.randrange(1000,9999)
- p=0
- print seed #this is test line, if you want to test this program, you can del the "#"
- while True:
- p=p+1
- guess0=raw_input('Please input a number form 1000 to 9999:')
- if re.search('[^0-9]',guess0) or guess0=='':
- print 'Not a number,and it would be counted as a try!'
- continue
- guess=int(guess0)
- if guess<1000 or guess>9999:
- print 'Invalid number you input,and it would be counted as a try!'
- continue
- if guess<seed:
- print 'More please'
- continue
- if guess>seed:
- print 'Less please'
- continue
- if guess==seed:
- time2=time.time()
- utime=str(int(time2-time1))
- file.write(utime+'\n')
- if p<=6:
- print 'You got it after',p,'tries','and',utime,'seconds,perfect!!'
- break
- if 6<p<=15:
- print 'You got it after',p,'tries','and',utime,'seconds,perfect!!'
- break
- else:
- print 'You got it after',p,'tries','and',utime,'seconds,perfect!!'
- break
- ask1=raw_input('Do you want to play again?(press "ENTER" to continue):')
- file.close()
- os.system('clear')
- continue
- elif(ask0=='2'):
- os.system('clear')
- list=[]
- file=open(path_home+'/.randata.log')
- file.seek(0)
- check=file.read()
- if(len(check)==0):
- print 'No record at all now!'
- continue
- else:
- p=0
- time_all=0
- file.seek(0)
- for line in file.readlines():
- list.append(int(line))
- p=p+1
- for time_single in list:
- time_all=time_all+time_single
- list.sort()
- if p==1:
- print 'There is only 1 record in log file!'
- print 'The record is',list[0],'second(s)'
- else:
- print 'There are',p,'records in log file!'
- print 'The best record is:',list[0],'second(s)'
- list.reverse()
- print 'The worst record is:',list[0],'second(s)'
- print 'The all time you played is:',time_all,'second(s)'
- print 'The average time you play is:',time_all/(p+0.0),'second(s)'
- file.close()
- print
- receive=raw_input('Press any "ENTER" to back to menu........')
- os.system('clear')
- continue
- elif(ask0=='3'):
- os.system('rm $HOME/.randata.log')
- os.system('clear')
- continue
- elif(ask0=='4'):
- break
复制代码 |
|