设为首页 收藏本站
查看: 781|回复: 0

[经验分享] 语句输出《Python for Beginners》学习笔记(3) 语句输出

[复制链接]

尚未签到

发表于 2017-5-6 10:52:52 | 显示全部楼层 |阅读模式
  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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373718-1-1.html 上篇帖子: 文件模块python使用dbm持久字典详解 文件模块 下篇帖子: 输出循环《Python for Beginners》学习笔记(4) 输出循环
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表