《Python for Beginners》为LearnStreet上的Python入门课程。本节主要学习内容为条件语句。
Lesson 4 Control Flow and Conditionals
1. 第一个Python函数
1 def check_wounds(): 2 #your code here 3 arms = 0 4 if arms == 1: 5 return "tis but a scratch" 6 elif arms == 0: 7 return "flesh wound" 8 else: 9 return "cross bridge" 10 11 #This is just for you to see what happens when the function is called 12 print check_wounds()
输出结果:
flesh wound
2. if语句
1 def always_true(): 2 if 1 == 1: 3 #insert your code here 4 return "complete" 5 6 #This is just for you to see what happens when the function is called 7 print always_true()
输出结果:
complete
3. 再来一个if语句
1 flag = "unchanged" 2 def always_false(): 3 global flag 4 if 1==2: 5 flag = "changed" 6 7 #This is just for you to see what happens when the function is called 8 always_false() 9 print flag
输出结果:
unchanged
4. else语句
1 def check_condition(): 2 #return this if the condition is True 3 ifRun = "if code block run" 4 5 #return this is if the condition is False 6 elseRun = "else code block run" 7 8 condition = 0 9 10 if condition == True: 11 #your code here 12 return ifRun 13 else: 14 #your code here 15 return elseRun 16 17 #This is just for you to see what happens when the function is called 18 print check_condition()
输出结果:
else code block run
5. elif语句
1 def colorful_conditions(): 2 3 color = "blue" 4 5 if color == "red": 6 return "first block" 7 elif color == "white": 8 return "second block" 9 elif color == "blue": 10 return "third block" 11 else: 12 return "fourth block" 13 14 #This is just for you to see what happens when the function is called 15 print colorful_conditions()
输出结果:
third block
6. if语句练习
问题:
When the string phrase is fewer than 30 characters long, return 1. When it is exactly 30 return 2, and when it is greater than 30 return 3.
代码:
1 def check_length(phrase): 2 # your if condition here 3 if len(phrase) < 30: 4 return 1 5 # your elif condition here 6 elif len(phrase) == 30: 7 return 2 8 # your else condition here 9 else: 10 return 3 11 12 #This is just for you to see what happens when the function is called 13 print check_length("hi, i am a phrase")
输出结果:
1
7. if语句练习2
1 """ 2 This function should check the value of the num variable and 3 return the string representation of the interval it is in Use if, elif, 4 and else statements to check if num falls in the range 1-5, 6-10, 5 11-15, or 16-20. Then return the correct interval as a string, like 6 "1-5", "6-10", "11-15", or "16-20". 7 """ 8 def check_interval(): 9 #Your code here 10 num = 5 11 if num < 6: 12 return "1-5" 13 elif num < 11: 14 return "6-10" 15 elif num < 16: 16 return "11-15" 17 else: 18 return "16-20" 19 20 #This is just for you to see what happens when the function is called 21 print check_interval()
输出结果:
1-5
8. and操作符
1 def should_eat(hungry, awake): 2 #your if statement here 3 if hungry and awake: 4 return "eat" 5 else: 6 return "don't eat" 7 8 #This is just for you to see what happens when the function is called 9 print should_eat(True, True)
输出结果:
eat
9. or操作符
1 def kitchen_or_bed(hungry, thirsty): 2 # your if statement here 3 if hungry or thirsty: 4 return "go to kitchen" 5 else: 6 return "go to bed" 7 8 #This is just for you to see what happens when the function is called 9 print kitchen_or_bed(True, True)
输出结果:
go to kitchen
10. 复习
1 def who_are_you(green, large): 2 # Write if-elif-else statements below 3 if green and large: 4 return "hulk" 5 elif green: 6 return "alien" 7 elif large: 8 return "elephant" 9 else: 10 return "Bruce Banner" 11 12 #This is just for you to see what happens when the function is called 13 print who_are_you(True, True)
输出结果:
hulk
总结:
1)注意缩进的格式
2)#开头为注释一行代码
3)注意函数定义以及条件语句后面的冒号
4)字符串可以直接作为函数的实参
(本文完)