下面是练习21-30,基于python3

 #ex21.py
1 def add(a, b):
print("ADDING %d + %d" %(a, b))
return a+b def subtract(a, b):
print("SUBTRACTING %d - %d" %(a, b))
return a-b def multiply(a, b):
print("MULTIPLYING %d * %d" %(a, b))
return a*b def divide(a, b):
print("DIVIDING %d / %d" %(a, b))
return a / b print("Let's do some math with just functions!") age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2) print("Age: %d, Height: %d, Weight: %d, IQ: %d"%(age,height,weight,iq)) # A puzzle for the extra credit, type it in anyway.
print("Here is a puzzle.") what = add(age, subtract(height, multiply(weight, divide(iq, 2)))) print("That becomes: ", what, "Can you do it by hand?")
 #ex24.py
1 print("Let's practice everything.")
print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.') poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
""" print("--------------")
print(poem)
print("--------------") five = 10 - 2 + 3 - 6
print("This should be five: %s" % five) def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates start_point = 10000
beans, jars, crates = secret_formula(start_point) print("With a starting point of: %d" % start_point)
print("We'd have %d beans, %d jars, and %d crates."%(beans, jars, crates)) start_point = start_point / 10 print("We can also do that this way:")
print("We'd have %d beans, %d jars, and %d crates." %secret_formula(start_point))
 #ex25.py
1 def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words def sort_words(words):
"""Sorts the words."""
return sorted(words) def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print(word) def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print(word) def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words) def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words) def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words) sentence = "All good things come to those who wait"
 #ex29.py
1 people = 20
cats = 30
dogs = 15 if people < cats:
print("Too many cats! The world is doomed!") if people > cats:
print("Not many cats! The world is saved!") if people < dogs:
print("The world is drooled on!") if people > dogs:
print("The world is dry!") dogs += 5 if people >= dogs:
print("People are greater than or equal to dogs.") if people <= dogs:
print("People are less than or equal to dogs.") if people == dogs:
print("People are dogs.")
 #ex30.py
1 people = 30
cars = 40
buses = 15 if cars > people:
print("We should take the cars.")
elif cars < people:
print("We should not take the cars.")
else:
print("We can't decide.") if buses > cars:
print("That's too many buses.")
elif buses < cars:
print("Maybe we could take the buses.")
else:
print("We still can't decide.") if people > buses:
print("Alright, let's just take the buses.")
else:
print("Fine, let's stay home then.")

最新文章

  1. salesforce 零基础学习(五十三)多个文件生成一个zip文件(使用git上封装的代码)
  2. Java开发环境搭建——Maven配置
  3. Shell.xaml
  4. CSDN 2013年度博客之星评选——分享几张厦门杭州的美图
  5. svn: warning: &#39;xxxxxx&#39; is already under version control
  6. C# - 事物回滚
  7. 在CentOS上安装Python
  8. 使用phpExcel向mysql数据库导入excel
  9. Python学习笔记 第一课 列表
  10. /storage/sdcard, /sdcard, /mnt/sdcard 三者的区别
  11. 用它解决大问题啦,STRACE应用
  12. Light OJ 1104 第六周F题
  13. 01_docker学习总结
  14. angularjs——路由篇
  15. 微信小程序电商实战(-)商城首页
  16. mysql优化的21条经验(转)
  17. 性能测试六:jmeter进阶之Cookie与header管理器
  18. delimiters 插值 选项
  19. gentoo AR9285 BCM57780 安装驱动
  20. JavaWeb基础—JS学习小结

热门文章

  1. Beetle简单构建TCP服务
  2. Foxit_PDF_Editor(特别版)-PDF文档编辑器 V2.21 V3.1
  3. codeforces#1215E. Marbles(状压DP)
  4. 如何判断一段程序是由C 编译程序还是由C++编译程序编译的
  5. qt 保存文件为utf8
  6. ofbiz框架以及核心原理介绍
  7. c# ASP.NET MVC easyui-filebox 图片上传和显示
  8. AppDomain (转)
  9. Tensorflow学习笔记3:卷积神经网络实现手写字符识别
  10. Nginx优化_定义对静态页面的缓存时间