zoukankan      html  css  js  c++  java
  • 猜数字 分类: python 小练习 python基础学习 2013-06-20 15:16 160人阅读 评论(0) 收藏

    使用raw_input()函数:

    import random
    v = random.randint(1,100)
    n = 1
    m = 100

    '''
    v -- 随机出来的期望数值
    u -- 用户输入的数值
    n -- 用户输入的边界最小值
    m -- 用户输入的边界最大值
    '''
    print '随机数是:',v

    while 1:
        print "please enter %d - %d" % (n,m)
        u = raw_input('my number:')

        if u.strip().isdigit(): #判断用户输入的内容是否全部由数字构成

            u= int(u)

            if u<n or u>m:
                print '输入范围错误,请重新输入‘

                continue
            if u>v:
                m=u
            if u<v:
                n=u
            if u==v:break
        else:
            print "请输入正确的整数"

    print 'you get it,value is %d' % v

    ------------------------------------------------------------------------------------------------------------

    ------------------------------------------------------------------------------------------------------------


    使用input()函数:


    import random
    v = random.randint(1,100)
    n = 1
    m = 100

    '''
    v -- 随机出来的期望数值
    u -- 用户输入的数值
    n -- 用户输入的边界最小值
    m -- 用户输入的边界最大值
    '''
    print '随机数是:',v

    while 1:
        print "please enter %d - %d" % (n,m)
        try:
            u = input('my number:')
        except:
            print "请输入正确的整数"
            continue

       
        if u<n or u>m:
            print '输入范围错误,请重新输入'
            continue
        if u>v:
            m=u
        if u<v:
            n=u
        if u==v:break
     

    print 'you get it,value is %d' % v


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Code Generation and T4 Text Templates
    Pros and Cons of T4 in Visual Studio 2008
    PSPInstance Object | Web Python
    Mod_Python中文文档
    [Medusa-dev] psp_handler
    Difference Between Mod_Python & Mod_Wsgi | eHow
    Mod_python: The Long Story
    HOWTO Use Python in the web — Python v3.0.1 documentation
    Js~对数组进行分组户数
    大叔也说并行和串行`性能提升N倍(N由操作系统位数和cpu核数决定)
  • 原文地址:https://www.cnblogs.com/think1988/p/4628153.html
Copyright © 2011-2022 走看看