• python之经典猜数字


    题目:猜数字
    1.让用户输入1-20,猜数字,可以猜5次。
    2.每次有提示,大了,或者小了!
    3.如果超过5次,提示game over.

    # !/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:Hiuhung Wan
    '''
    题目:猜数字
    1.让用户输入1-20,猜数字,可以猜5次。
    2.每次有提示,大了,或者小了!
    3.如果超过5次,提示game over.
    '''
    import random
    init_number = random.randint(1, 20)   # 准确的数字
    max_count = 5      # 最大猜的次数
    count = 0          # 计数器
    
    while count < max_count:
        try:
            temp = int(input("请输入一个整数(1-20):"))
            if temp == init_number:
                print("竟然猜对了!")
                break
            elif temp < init_number:
                print("小了点")
            else:
                print("大了点")
            count += 1
        except ValueError:
            print("给我个整数,谢谢")
    else:
            print("猜对是不可能猜对的,这一辈子都不可能猜对的,数字是%s。" % init_number)
    

      

    效果如下:

    请输入一个整数(1-20):12.3
    给我个整数,谢谢
    请输入一个整数(1-20):12
    竟然猜对了!
    
    Process finished with exit code 0
    

      

    请输入一个整数(1-20):12.3
    给我个整数,谢谢
    请输入一个整数(1-20):12
    大了点
    请输入一个整数(1-20):6
    大了点
    请输入一个整数(1-20):3
    大了点
    请输入一个整数(1-20):1
    竟然猜对了!
    
    Process finished with exit code 0
    

      

    请输入一个整数(1-20):3
    小了点
    请输入一个整数(1-20):3
    小了点
    请输入一个整数(1-20):3
    小了点
    请输入一个整数(1-20):3
    小了点
    请输入一个整数(1-20):3
    小了点
    猜对是不可能猜对的,这一辈子都不可能猜对的,数字是17。
    
    Process finished with exit code 0
    

      

  • 相关阅读:
    SNMP简介
    命令模式
    牛顿法
    仿射函数
    schedule
    命令模式
    牛顿法
    schedule
    仿射函数
    适配器模式
  • 原文地址:https://www.cnblogs.com/hiuhungwan/p/10549130.html
Copyright © 2020-2023  润新知