Conditionals & Control Flow
37 介绍了条件语句if 38 if的格式如下, 比如
[python]
if 8 < 9:
print "Eight is less than nine!"
3 另外还有这elif 以及else,格式如下
[python]
if 8 < 9:
print "I get printed!"
elif 8 > 9:
print "I don't get printed."
else:
print "I also don't get printed!"
4 练习:设置变量response的值为'Y'
[python]
response = 'Y'
answer = "Left"
if answer == "Left":
print "This is the Verbal Abuse Room, you heap of parrot droppings!"
Will the above print statement print to the console?
Set response to 'Y' if you think so, and 'N' if you think not.
第十节
1 介绍了if的格式
[python]
if EXPRESSION:
# block line one
# block line two
# et cetera
2 练习:在两个函数里面加入两个加入条件语句,能够成功输出
[python]
def using_control_once():
if 1 > 0:
return "Success #1"
def using_control_again():
if 1 > 0:
return "Success #2"
print using_control_once()
print using_control_again()
39 介绍了else这个条件语句
2 练习:完成函数里面else条件语句
[python]
answer = "'Tis but a scratch!"
def black_knight():
if answer == "'Tis but a scratch!":
return True
else:
return False # Make sure this returns False
def french_soldier():
if answer == "Go away, or I shall taunt you a second time!":
return True
else:
return False # Make sure this returns False
40 介绍了另外一种条件语句elif的使用
2 练习:在函数里面第二行补上answer > 5, 第四行补上answer < 5 , 从而完成这个函数
[python]
def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
第十三节
1 练习:利用之前学的比较以及连接符以及条件语句补全函数。所有的都要出现至少一次
[python]
def the_flying_circus():
# Start coding here!
if 1 < 2 and not False:
return True
elif 1 == 1 or 1 != 2 or 1 < 2 or 1 <= 2 or 1 > 2 or 1 >= 2:
return True
else:
return False