笨办法学Python(learn python the hard way)--练习程序21-30
下面是练习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.")
笨办法学Python(learn python the hard way)--练习程序21-30的更多相关文章
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本
黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- 笨办法学Python - 习题1: A Good First Program
在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...
- 笨办法学python 13题:pycharm 运行
笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...
- 笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘
笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:xaln 怎样阅读本书 由于本书结构独特,你必须在学习时遵守几条规则 录入所有代码,禁止复制粘贴 一字不差地录入代码 ...
- 笨办法学Python 3|百度网盘免费下载|新手基础入门书籍
点击下方即可百度网盘免费提取 百度网盘免费下载:笨办法学Python 3 提取码:to27 内容简介: 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用. ...
- 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程
<笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...
- 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码
笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...
- 《笨办法学Python 3》python入门书籍推荐|附下载方式
<笨办法学Python 3>python入门书籍免费下载 内容简介 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...
随机推荐
- Java基础/阿里巴巴Java开发手册
阿里巴巴Java开发手册 序号 文档名及下载地址 1 阿里巴巴Java开发手册v1.2.0 2 <阿里巴巴Java开发手册>(终极版) 为什么阿里巴巴禁止工程师直接使用日志系统(Log4j ...
- Tensorflow之CNN卷积层池化层padding规则
padding的规则 · padding=‘VALID’时,输出的宽度和高度的计算公式(下图gif为例) 输出宽度:output_width = (in_width-filter_wid ...
- 红黑树插入操作---以JDK 源码为例
红黑树遵循的条件: 1.根节点为黑色. 2.外部节点(叶子节点)为黑色. 3.红色节点的孩子节点为黑色.(由此,红色节点的父节点也必为黑色) 4.从根节点到任一外部节点的路径上,黑节点的数量相同. 节 ...
- 解决浏览器打开网页后提示“dns_probe_possible”的方法
使用浏览器浏览网页时偶尔会遇到无法上网且浏览器提示:DNS_PROBE_POSSIBLE 一般有三种情况会导致这样的故障: 1.网络协议出现故障,也就是常说的 DNS 设置问题 2.浏览器中设置问题, ...
- [Python3 练习] 004 水仙花数
题目:水仙花数 (1) 描述 水仙花数各位的数字的立方之和等于自身 如 153 为水仙花数,因为 153 = 1^3 + 5^3 + 3^3 (2) 要求 找到所有的三位数的水仙花数 (3) 程序 # ...
- poj-2516.minimum cost(k次费用流)
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19883 Accepted: 7055 Des ...
- 循环结构 :while
循环结构 :while 循环四要素: 1.初始化条件 2.循环条件 3.循环体 4.迭代条件 格式: 1.初始化条件 while(2.循环条件){ 3.循环体 4.迭代条件 } public clas ...
- js数组中的引用类型
我们看一下这个例子: let a={tile:'深复制'}; let b=a; a.title='浅复制'; 那么我们会获得两个对象,一个a,一个b,a的title是浅复制,b的title是深复制.但 ...
- 搜索(DFS)---填充封闭区域
填充封闭区域 130. Surrounded Regions (Medium) For example, X X X X X O O X X X O X X O X X After running y ...
- 修改url,
第一种场景: 无论url怎么变,表单里面的url始终不变 http://127.0.0.1:8000/CC/indexssssssssssssssssss/ url(r'^indexsssssssss ...