代码示例
def clinic(): print "you've just entered the clinic!" print "Do you take the door on the left or on the right?" answer = raw.input(Type left or right and hit the enter).lower() if answer == "left" or answer =="l": print "balabala..." elif answer =="right" or answer =="r": print "balabala..." else: print "balabala..." clinic() clinic()control flow: comparator
==(等于)、<、<=、>、>=、!=(不等于)
注意:其中“小于等于”是“小于”或“等于”Boolean operators: and or not,真值表
- 当程序中同时出现and or not时,程序执行的优先级是not>and>or,如:False and not False or True的结果是True;
- 可以使用括号()按照自己的目的调整程序执行顺序,如:False or not (not True and False)的结果是True;
- 在进行判断正误的时候,除了简单的数字比较,还对比字符串等,如“apple”!=“pear”
if…elif…else
score = raw_input("score:") score = int(score) if (score >=90) and (score <=100): print “A" elif (score >=80) and (score <90): print "B" elif (score >=70) and (score < 80): print "C" else: print "D"
CodeCademy | Python | 5. Conditionals & Control flow
最新推荐文章于 2024-12-03 14:44:57 发布
本文通过一个简单的Python代码示例介绍了如何使用if...elif...else条件语句及布尔运算符来实现程序流程控制。文章详细解释了各种比较运算符(如==、<、<=、>、>=、!=)和布尔运算符(如and、or、not)的用法,并展示了如何根据输入的分数进行不同等级的评价。
433

被折叠的 条评论
为什么被折叠?



