python学习1

b=0
num=23
succ=False
maxTryTime=3
while b<maxTryTime :
    a=int( input('Wrtie a Number(10-30),you have '+str(maxTryTime-b)+' times :'))
    
    if a==num:
         print 'you got it'
         succ=True
         break        
    elif  a<   num:
         print 'No,it is a bigger num'
    else:
         print 'No,it is a smaller num'       


    b=b+1
 
if  not  succ :
    print 'you failed'
else:
    print 'you got it by '+str(b+1)+' times'
input()

  如果输入的不为数字,程序会直接退出,没有容错性

修改如下:

b=0
num=23
succ=False
maxTryTime=3
 
while b<maxTryTime :
    
    str1=raw_input('Wrtie a Number(10-30),you have '+str(maxTryTime-b)+' times :')
    if not str1.isdigit():
        print str1+' is not a num !!please write a num'
    else:
        a=int(str1)
        
        if a==num:
             print 'you got it'
             succ=True
             break        
        elif  a<   num:
             print 'No,it is a bigger num'
        else:
             print 'No,it is a smaller num'       
   
               


    b=b+1
 
if  not  succ :
    print 'you failed'
else:
    print 'you got it by '+str(b+1)+' times'
input()

  

这里再记一下raw_input 与 input 的区别

 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。而对于 input() ,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来

原文地址:https://www.cnblogs.com/zhshlimi/p/5646187.html