zoukankan      html  css  js  c++  java
  • 猜年龄

    #!/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)
  • 相关阅读:
    python中的反射
    ZOJ 3827 Information Entropy 水
    我的软考之路(七)——数据结构与算法(5)之查找
    nginx.conf 集群完整配置
    恼人的函数指针(二)
    C语言100个经典的算法
    spring事务心得积累
    Vue报错:OPTIONS 405 Method Not Allowed以及CORS跨域错误
    IDA脚本dump内存的示例
    lightProxy
  • 原文地址:https://www.cnblogs.com/godspeed034/p/7245608.html
Copyright © 2011-2022 走看看