笨办法学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入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...
随机推荐
- 45 MySQL自增id
45 MySQL自增id 表定义自增id 说到自增id,前面提到mysql的自增id不连续,当表定义的自增值达到上限后的逻辑是:再申请下一个id时,得到的值保持不变 ; insert into t v ...
- 转 linux查看文件前几行和后几行的命令
可以使用head(查看前几行).tail(查看末尾几行)两个命令.例如:查看/etc/profile的前10行内容,应该是:# head -n 10 /etc/profile查看/etc/profil ...
- Java基础/发起http和https请求
Java中发起http和https请求 一般调用外部接口会需要用到http和https请求. 本案例为:前后端完全分离,前端框架(React+Mobx+Nornj),后端(Go语言). 面临问题:跨域 ...
- [Python3] 019 函数:确认过参数,返回对的值
目录 0. 函数简介 1. 初识函数 2. 函数的参数与返回值 少废话,上例子 3. 查找函数的帮助文档 4. 函数的参数 (1) 参数分类 (2) 结构介绍 1) 普通参数 2) 默认参数 3) 关 ...
- MySQL-第十三篇使用ResultSetMetaData分析结果集
1.Result里面包含了一个getMetaData()方法,该方法返回该ResultSet对应的ResultSetMetaData对象. 2.ResultSetMetaData包含的方法: 1> ...
- servlet到springmvc的演进
1.简单看看servlet 1.1.servlet继承关系 先看看下面servlet的这个继承关系,有点印象即可(可以暂时忽略ServletConfig,这个接口就是让我们可以从web.xml文件中拿 ...
- 手写ORM持久层框架(转)
工程结构: 本文测试的数据库为: 其中student的表结构为: 表数据: 配置文件 DB2.properties driver=com.mysql.jdbc.Driver url=jdbc\:mys ...
- 利用 Python 进行批量更改文件后缀
利用 Python 进行批量更改文件后缀 代码 import os files = os.listdir('.') for file_name in files: portion = os.path. ...
- javascript基础总汇
## javaScript是什么:1.JavaScript 运行在客户端(浏览器)的编程语言2.用来给HTML网页增加动态功能3.用来给HTML网页增加动态功能.4.Netscape在最初将其脚本语言 ...
- Linux awk抓取IP的两种方式
ip addr show ens33 | awk -F "[ /]+" '/inet /{print $3}' 或 ifconfig ens33 | awk -F "[ ...