语句输出《Python for Beginners》学习笔记(3) 语句输出

PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘。目前又不当COO,还是得用心记代码哦!

    《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)字符串可以直接作为数函的实参

    (本文完)

文章结束给大家分享下程序员的一些笑话语录: 警告
有一个小伙子在一个办公大楼的门口抽着烟,一个妇女路过他身边,并对他 说, “你知道不知道这个东西会危害你的健康?我是说, 你有没有注意到香烟 盒上的那个警告(Warning)?”
小伙子说,“没事儿,我是一个程序员”。
那妇女说,“这又怎样?”
程序员说,“我们从来不关心 Warning,只关心 Error”

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值