除HelloWorld外的第一个Python程序——数字猜谜游戏

今天又打开了《简明Python教程》的网页,正式开始了对Python的学习,于是把这个略微还像样的程序贴上来,以示鼓励 *_*:

python 代码
 
  1. #! /usr/bin/python  
  2. # filename: numGuess.py  
  3.   
  4. # define function numGuess  
  5. # number - the number to guess, times - the max times allowing to guess  
  6. def numGuess(number, times):  
  7.     for i in range(0, times):  
  8.         guess = raw_input('Enter an interger or exit(e):')  
  9.         try:  
  10.              strGuess = str(guess)              
  11.              if strGuess == 'e\r':  
  12.                  print 'User quit the game.'  
  13.                  return                
  14.              try:  
  15.                 intGuess=int(guess)   
  16.                 if intGuess < number:  
  17.                     print 'No, it is a little higher than that.'  
  18.                 elif int(guess) > number:  
  19.                     print 'No, it is a little lower than that'  
  20.                 else :  
  21.                     print 'Congratulations, you guessed it.'  
  22.                     return  
  23.              except:  
  24.                 print 'Wrong input.'  
  25.         except :  
  26.              print 'Wrong input.'  
  27.     print 'Time is exhausted. Game over!'      
  28.   
  29. # call function numGuess(number, times)  
  30. numGuess(20, 4)  


附《简明Python教程》网址: www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/index.html

你可能感兴趣的:(html,游戏,python,网页游戏)