代码示例
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-06-14 09:36:50 发布