除HelloWorld外的第一个Python程序——数字猜谜游戏
今天又打开了《简明Python教程》的网页,正式开始了对Python的学习,于是把这个略微还像样的程序贴上来,以示鼓励 *_*:python 代码
[*]#! /usr/bin/python
[*]# filename: numGuess.py
[*]
[*]# define function numGuess
[*]# number - the number to guess, times - the max times allowing to guess
[*]def numGuess(number, times):
[*] for i in range(0, times):
[*] guess = raw_input('Enter an interger or exit(e):')
[*] try:
[*] strGuess = str(guess)
[*] if strGuess == 'e\r':
[*] print 'User quit the game.'
[*] return
[*] try:
[*] intGuess=int(guess)
[*] if intGuess < number:
[*] print 'No, it is a little higher than that.'
[*] elif int(guess) > number:
[*] print 'No, it is a little lower than that'
[*] else :
[*] print 'Congratulations, you guessed it.'
[*] return
[*] except:
[*] print 'Wrong input.'
[*] except :
[*] print 'Wrong input.'
[*] print 'Time is exhausted. Game over!'
[*]
[*]# call function numGuess(number, times)
[*]numGuess(20, 4)
附《简明Python教程》网址:www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/index.html
页:
[1]