002. Python入门经典之二: 第四章演练分支循环处理异常

使用==  !=  <   > 做比较:

#coding:utf-8
#比较两个字符串
a="hello"
b="Hello"

print (a==b)
#输出结果 False

#比较两个序列
apples=["This","is","Apples"]
chuiZi=["This","is","ChuiZi"]
print (apples==chuiZi)
#输出结果 False
print (len(apples)==len(chuiZi))
#输出结果 True
#比较字典
tuesday_breakfase_sold={"pancakes":10,"french toast":4, "bagels":12}
thursday_breakfast_sold={"hello":10,"Hi":5,"Hm":3}
print(tuesday_breakfase_sold==thursday_breakfast_sold)
#输出结果 False
#在python中用于比较的符号
#==    !=    <    >
#使用lower()和Upper()方法
print ("Hello".lower() == "Hello".lower())
#输出结果:  True

#使用not来取反
print (not True)
#输出结果 False

#not的其它用法
print (not 5 > 2)
#输出结果 False   #先对5进行取反, 然后得到False, False为0 , 所以0>2, 依旧是false
print (not 5 < 2)
#输出结果 True

 使用分支和循环

#使用分支
    #if的使用
if 1>2:
    print ("No it is not!")
if 2>1:
    print ("Yes it is!")
elif 3<2:
        print ("Go to Moer!")


OJ_price = 2.5;
if OJ_price<1.25:
     print ("Get one, I'm thirsty")
elif OJ_price<=2.0:
      print ("Ummm...sure, but I'll drink it slowly")
else:
      print ("I don't have enough money. Never mind")
#输出结果: I don't have enough money. Never mind


#使用循环 while . . .     和 for;  break 跳出循环
i=3;
while   i>0:
    print ("Lift off in :%d" % i)
    i=i-1;
'''输出结果:
Lift off in :3
Lift off in :2
Lift off in :1
'''
#使用for, 它有两个重载
for i in range(3,0,-1):
    print ("T-minus:%d" % i)
#i的初始值为3, 每次循环将其-1 , 当小于0时候停止
#输出结果: T-minus:3   T-minus:2    T-minus:1

for i in range(3):
    print ("T-minus:%d" % i)
#输出结果:   T-minus:0   T-minus:1    T-minus:2

#使用break 跳出循环
'''age=0
while True:
    how_old = input ("Enter you age:")
    if how_old == "No":
        print("Don't be ashamed of your age!")
        break;
    num=int(how_old)
    age=age+num
    print ("you age is :%d " % age)
    #print (age)
    print ("That is old!")
'''
#使用continue继续循环
for food in ("pate","cheese", "rotten apples","crackers", "whip cream","tomato soup"):
    if food [0:6] == "rotten":
        continue;
        print("Hey you can %s" % food)
    elif food[0:2] == "cr":
        print ("Hey you can %s" % food)
    else :
        print ("You  chooes %s" % food)
#输出结果: 可以看到rotten apples永远不会被打出
'''You  chooes pate
You  chooes cheese
Hey you can crackers
You  chooes whip cream
You  chooes tomato soup'''

 处理异常:

#异常处理
fridge_contents={"egg":8,"mushroom":20,"pepper":3,"cheese":2,"tomato":4,"milk":13}
try:
    if fridge_contents["orange juice"] >3 :
        print ("Sure, Let's have some juice!")
except: #except KeyError
    print ("No!~~~~")
#未使用try...except之前会抛出异常 : KeyError: 'orange juice'

#异常的第二个示例:
#创建异常以及对异常的说明
fridge_contents={"egg":8,"mushroom":20,"pepper":3,"cheese":2,"tomato":4,"milk":13}
try:
    if fridge_contents["orange juice"] >3 :
        print ("Sure, Let's have some juice!")
except (KeyError) as error: #except KeyError
    print ("No!~~~~ %s" % error )
#运行结果: No!~~~~ 'orange juice'
#在字典fridge_contents中没有orange juice关键字, 所以会抛出一个KeyError异常, 然后指定一个名称error, Python将keyError的值
#赋给error, 赋值过程通过as关键字

#使用元组包含所有的异常, 和使用pass跳过(忽略)异常, 就相当于throw new Expression()抛出异常给更高一层
fridge_contents={"egg":8,"mushroom":20,"pepper":3,"cheese":2,"tomato":4,"milk":13}
try:
    if fridge_contents["orange juice"] >3 :
        print ("Sure, Let's have some juice!")
except (KeyError, TypeError) as error: #except KeyError
    print ("No!~~~~ %s" % error )
except (TypeError):
    pass

 

posted on 2017-02-23 16:48  印子  阅读(129)  评论(0)    收藏  举报

导航