除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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值