猜年龄

#!/usr/bin/env python
# -*- coding:utf-8 -*-  
# by wk

'''
说明:
1.    允许用户最多尝试3次
2.    每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
3.    如何猜对了,就直接退出

'''

def guessage(my_age):
    flag = ''
    while True:
        count = 3
        for i in range(3):                  #循环3次
            try:
                age = int(input('please enter age: '))
                if age > my_age:
                    count -= 1              #统计输错次数
                    print('you guess too big, you have %s chance' %count)
                    if i == 2:              #猜错3次后询问还继续猜不
                        # print('you have no chioce')
                        flag = input('Do you want try again Y/N ? ')
                elif age < my_age:
                    count -= 1              #统计输错次数
                    print('you guess too smaill, you have %s chance' %count)
                    if i == 2:              #猜错3次后询问还继续猜不
                        # print('you have no chioce')
                        flag = input('Do you want try again Y/N ? ')
                else:
                    print('Congratulations! you guess it !')      #猜对了退出
                    flag = 'N'
                    break;
            except:                         #如果输入的不是数字,提示请输入数字
                count -= 1
                print('Not number type !!! please enter number, you have %s chance' %count)
        if flag == 'Y' or flag == 'y':
            continue
        elif flag == 'N' or flag == 'n':
            break
        else:
            print('Wrong choice !!!')       #如果输入的不是Y,y,N,n,直接退出
            break

if __name__ == '__main__':
    my_age = 32
    guessage(my_age)

 

posted @ 2017-07-27 16:34  GodSpeed034  阅读(116)  评论(0编辑  收藏  举报