决策

决策
1、根据True和False做出决策
==,!=,>,<,>=,<=
比较两个值是否相等,比较两个字符串的内容是否相同:
序列:序列也可用双等号比较:每个序列中同一位置的每个元素都相同,则两个序列相等;若序列中的元素相同,但顺序不同,则两个序列不相等。
字典:字典中的每个键和值必须与另一字典中的键和值一一对应,则两个字典相等;
比较两个值是否不相等,比较两个值的大小:
字母值的比较:“A"<"Z"<"a"<"z"
字符串:特殊方法lower和upper
lower:将其调用的字符串中的所有字母都变为小写;upper:将其调用的字符串中的所有字母都变为大写。
a="True or False"
a.lower() ----> 'true or false'
a.upper() ----> 'TRUE OR FSLSE'
真值,假值和取反:
not运算符用于任何得出True和False结果的测试中,(任何非0的值都被看做True),因此在预期之外或不一定有意义的情况下使用not。
not "A" < 3 ----> False
not 5 > 2 ----> False
若需要将多个运算结果放在一起,使用and或or
2、做出决策
if判断条件:
if 1 > 2:
print("it is false")
if 1 < 2:
print("it is true")
python中的冒号":"它指出python进入了程序中与其余部分相对独立的一部分。此时缩进很重要;python通过缩进知道一个特定的代码块与周围的代码保持独立。
嵌套:
>>>omelet_ingredients={"egg":2,"mushroom":5,"pepper":1,"cheese":1,"milk":1}
>>>fridge_contents={"egg":10,"mushroom":20,"pepper":3,"cheese":3,"tomato":1,"milk":15}
>>>have_ingredients=[False]
>>>if fridge_contents["egg"] > omelet_ingredients["egg"]:
have_ingredients[0] = [True]
have_ingredients.append("egg")
>>>print(have_ingredients) ----> [[True], 'egg']
(注:have_ingredients=[False]:have_ingredients的第一个元素为False
>>>if fridge_contents["mushroom"] > omelet_ingredients["mushroom"]:
if have_ingredients[0] == False:
have_ingredients[0] = True
have_ingredients.append("mushroom")
>>>print(have_ingredients) ----> [[True], 'egg', 'mushroom']
(注:如果第一个if为真,同级的第二个条件将会被执行;比较的结构决定了其下面缩进的代码是否被执行,同级别的以及上层代码不会停止(除非特殊情况,如一个错误或一个阻止程序继续运行的条件))
结合if与elif使用:
price=1.5
if price < 1.25:
print("buy one egg")
elif price > 1:
print("buy one apple")
elif price < 2:
print("buy one tomato")
运行结果:buy one apple
(注:elif仅当前面的条件不满足时,才会被执行:(当if不满足时执行第一个elif,当第一个elif不满足时才会执行第二个elif))
结合if与elif与else使用:
price=1.5
if price < 1.25:
print("buy one egg")
elif price < 1:
print("buy one apple")
else:
print("buy one tomato")
运行结果:buy one tomato
(注:else的使用:当前面的条件都没有返回True的情形下(if和elif都没有返回为True时,执行else))
2>循环
while和for
while循环:
i=5
while i > 0:
print("list in")
print(i)
i=i-1
打印结果:
list in
5
list in
4
list in
3
list in
2
list in
1
for循环:
for i in range(10,0,-1):
print("list in")
print(i)
打印结果:
list in
5
list in
4
list in
3
list in
2
list in
1
终止循环:
使用break语句退出无穷循环:
age=0
while True:
how_old=input("enter your age:")
if how_old=="No":
print("donnot be ashamed of your age")
break
num=int(how_old)
age=age+num
print("your age is :")
print(age)
print("that is old")
运行:
enter your age:1
your age is :
1
that is old
enter your age:2
your age is :
3
that is old
enter your age:3
your age is :
6
that is old
enter your age:No
donnot be ashamed of your age
(注:如果使用break,它仅从最近的一个循环中退出。如果有一个while循环中,包含一个缩进的for...in循环,for...in中的break将不会退出while循环)
while循环和for...in循环在末端都可以有一个else:语句,但它仅在循环不是由break语句退出退出时才会被执行;这种情形下,else:叫做done或on_completion
循环使用else:
for food in ("pate","cheese","crackers","yogurt"):
if food =="yogurt":
break
else:
print("there is no yogurt")
运行结果:(空的,break退出循环)
for food in ("pate","cheese","crackers"):
if food =="yogurt":
break
else:
print("there is no yogurt")
运行结果:there is no yogurt
用continue继续循环:
for food in ("pate","cheese","rotten apples","crackers","cream","yogurt"):
if food[0:6]=="rotten":
continue
print("you can eat %s" % food)
运行结果:
you can eat pate
you can eat cheese
you can eat crackers
you can eat cream
you can eat yogurt
(使用了if来判断food列表中的每一项的第一部分是否包含字符串rotten,所以"rotten apples"元素会被continue跳过,其余的元素会被打印出来)
处理错误:
使用try:语句:
try:语句后面可以跟一个expect:语句,每一个expect:语句都处理错误,错误也被称作异常;当对try语句中的代码求值时会抛出异常,而不是使程序失败。
创建异常及对异常的说明:
frige_contents={"egg":8,"mushroom":20,"pepper":3,"cheese":2,"tomato":4,"milk":13}
try:
if frige_contents["orange juice"] > 3 :
print("let's have some juice")
except (KeyError)as error:
print("there is no %s" % error)
运行结果:there is no 'orange juice'
用相同的方式处理多种异常,可以用一个元组包含有关的所有异常类型:
....
except (KeyError,TypeError)as error:
....
如果需要处理一个异常,但是希望以什么都不做的方式处理它,可通过特殊词pass忽略这种情形:
frige_contents={"egg":8,"mushroom":20,"pepper":3,"cheese":2,"tomato":4,"milk":13}
try:
if frige_contents["orange juice"] > 3 :
print("let's have some juice")
except (KeyError)as error:
print("there is no %s" % error)
except (TypeError):
pass
运行结果:there is no 'orange juice'
另:else:语句可以放在try:代码的末端。在没有捕捉到任何异常时,else:将被执行。
posted @ 2018-08-06 21:14  Sky-wings  阅读(147)  评论(0编辑  收藏  举报