python考试代码复制_笨办法学Python 习题 26: 恭喜你,现在可以考试了! 错误代码下载链接...-程序员宅基地

技术标签: python考试代码复制  

你已经差不多完成这本书的前半部分了,不过后半部分才是更有趣的。你将学到逻辑,并通过条件判断实现有用的功能。

在你继续学习之前,你有一道试题要做。这道试题很难,因为它需要你修正别人写的代码。当你成为程序员以后,你将需要经常面对别的程序员的代码,也许还有他们的傲慢态度,他们会经常说自己的代码是完美的。

这样的程序员是自以为是不在乎别人的蠢货。优秀的科学家会对他们自己的工作持怀疑态度,同样,优秀的程序员也会认为自己的代码总有出错的可能,他们会先假设是自己的代码有问题,然后用排除法清查所有可能是自己有问题的地方,最后才会得出“这是别人的错误”这样的结论。

在这节练习中,你将面对一个水平糟糕的程序员,并改好他的代码。我将习题 24 和 25 胡乱拷贝到了一个文件中,随机地删掉了一些字符,然后添加了一些错误进去。大部分的错误是 Python 在执行时会告诉你的,还有一些算术错误是你要自己找出来的。再剩下来的就是格式和拼写错误了。

所有这些错误都是程序员很容易犯的,就算有经验的程序员也不例外。

你的任务是将此文件修改正确,用你所有的技能改进这个脚本。你可以先分析这个文件,或者你还可以把它像学期论文一样打印出来,修正里边的每一个缺陷,重复修正和运行的动作,直到这个脚本可以完美地运行起来。在整个过程中不要寻求帮助,如果你卡在某个地方无法进行下去,那就休息一会晚点再做。

就算你需要几天才能完成,也不要放弃,直到完全改对为止。

最后要说的是,这个练习的目的不是写程序,而是修正现有的程序,你需要访问下面的网站:

�6�1 http://learnpythonthehardway.org/book/exercise26.txt

从那里把代码复制粘贴过来,命名为 ex26.py ,这也是本书唯一一处允许你复制粘贴的地方。

常见问题回答

一定要 import ex25.py 吗?移除对它的引用也可以吧?

怎样都可以。不过这个文件里会用到 ex25 中的函数,你可以试着移除引用看看会怎样。

我可以边修正代码边运行吗?

当然可以。这样的事情就是要计算机帮忙,多多益善。

试题内容如下: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.poop(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)

print "Let's practice everything."

print 'You\'d need to know \'bout escapes with \\ that do  newlines and \t tabs.'

poem = """

\tThe lovely world

with logic so firmly planted

cannot discern  the needs of love

nor comprehend passion from intuition

and requires an explantion

\t\twhere there is none.

"""

print "--------------"

print poem

print "--------------"

five = 10 - 2 + 3 - 5

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 jeans, %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 crabapples." % secret_formula(start_pont

sentence = "All god\tthings come to those who weight."

words = ex25.break_words(sentence)

sorted_words = ex25.sort_words(words)

print_first_word(words)

print_last_word(words)

.print_first_word(sorted_words)

print_last_word(sorted_words)

sorted_words = ex25.sort_sentence(sentence)

prin sorted_words

print_irst_and_last(sentence)

print_first_a_last_sorted(senence)

修改后# -*- coding:utf-8 -*-

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)    # 取第一个字母 单词拼写错误

return word # print 更改 return,否则会返回none

def print_last_word(words):

"""Prints the last word after popping it off."""

word = words.pop(-1)    # 取最后一个字母

return word # print 更改 return,否则会返回none

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)

return print_first_word(words)

return 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)

return print_first_word(words)

return print_last_word(words)

print "Let's practice everything."

print 'You\'d need to know \'bout escapes with \\ that do  newlines and \t tabs.'

poem = """

\tThe lovely world

with logic so firmly planted

cannot discern  the needs of love

nor comprehend passion from intuition

and requires an explantion

\t\twhere there is none.

"""

print "--------------"

print poem

print "--------------"

five = 10 - 2 + 3 - 5

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 jeans, %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 crabapples." % secret_formula(start_point)  #少了一个) start_point平措

sentence = "All god\tthings come to those who weight."

words = break_words(sentence)

sorted_words = sort_words(words)

print_first_word(words)

print_last_word(words)

print_first_word(sorted_words) #print都了一个.

print_last_word(sorted_words)

sorted_words = sort_sentence(sentence)

print sorted_words

print_first_and_last(sentence)  #单词拼写错误

print_first_and_last_sorted(sentence)    #单词拼写错误

运行结果Let's practice everything.

You'd need to know 'bout escapes with \ that do

newlines and  tabs.

--------------

The lovely world

with logic so firmly planted

cannot discern

the needs of love

nor comprehend passion from intuition

and requires an explantion

where there is none.

--------------

This should be five: 6

With a starting point of: 10000

We'd have 5000000 jeans, 5000 jars, and 50 crates.

We can also do that this way:

We'd have 500000 beans, 500 jars, and 5 crabapples.

['All', 'come', 'god\tthings', 'those', 'to', 'weight.', 'who']

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39962356/article/details/112904996

智能推荐

RabbitMQ:发布确认模式_rabbitmq发布者确认-程序员宅基地

文章浏览阅读2.2k次,点赞9次,收藏12次。生产者把信道设置成为confirm(确认)模式,一旦信道进入confirm模式,所有在这个信道上面发布的消息都会被指定唯一的一个ID(ID从1开始).一旦消息被投递到所有匹配的队列以后,broker就会发送一个确认给生产者(包含ID),这样使得生产者知道消息已经正确到底目的队列了。如果消息和队列是可持久化的,那么确认消息就会在消息被写入磁盘以后发出,broker回传给生产者的确认消息中delivery-tag包含了确认消息的序列号。_rabbitmq发布者确认

abb机器人工具坐标系设定方式_实用 | ABB机器人如何设定工具坐标系?-程序员宅基地

文章浏览阅读5.7k次。1、机器人默认坐标系系统自带的TCP坐标原点在第六轴的法栏盘中心,垂直方向为Z轴,符合右手法则。注意:在设置TCP座标的时候一定要把机器人的操作模式调到“手动限速模式。2、设定工具坐标系的作用当机器人夹具被更换,重新定义TCP后,可以不更改程序,直接运行。当安装新夹具后就必需重新定义坐标系。否则会影响机器人的稳定运行。3、工具坐标系定义原理(1)在机器人工作空间内找一个精确尖锐的的固定点作为参考点..._abb机器人工具坐标系标定的过程

最小操作次数 c语言,力扣(LeetCode)刷题,简单题(第22期)-程序员宅基地

文章浏览阅读332次。目录力扣(LeetCode)定期刷题,每期10道题,业务繁重的同志可以看看我分享的思路,不是最高效解决方案,只求互相提升。试题要求如下: 解答思路:1、使用中序遍历把结果存在数组中;2、用双指针(即下标),来找对应结果是否存在 i指向最小,j指向最大。若i+j小于目标值,那么i++,i+j大于>目标值,则j--。回答(C语言):#define Maxsize 10000void inorde..._leetcode 2702.使数字变为非正数的最小操作次数

Exsi7.0在256GB以下存储设备安装无可用空间原因和解决方案_不存在具有可用空间的设备-程序员宅基地

文章浏览阅读8.5k次。Exsi7.0在256GB以下存储设备安装无可用空间原因和解决方案要解决OSDATA分区过大的问题,ESXI7.0,第一次进入安装时,按shift + O在显示的cdromBoot runweasel后输入autoPartitionOSDataSize=8192 注意大小写8192表示指定OSDataSzie为8GB,再按enter回车进行正常安装即可;对比6.7系统,给出8GB完全可以满足VMware tools、scratch以及coredump空间的需求。这样指定了空间大小,就不存_不存在具有可用空间的设备

设置ubuntu 默认不启动图形界面_ubuntu 不启动图形界面-程序员宅基地

文章浏览阅读1k次。以在控制台上输入命令sudo gdm或sudo startx。把 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"或 GRUB_CMDLINE_LINUX_DEFAULT="text"如果值为/usr/sbin/gdm,则进入图形界面。_ubuntu 不启动图形界面

江苏大学考研计算机专业目录,江苏大学考研专业目录-程序员宅基地

文章浏览阅读97次。出国留学考研网为大家提供江苏大学修订2017年博士、考研招生简章及专业目录的通知,更多考研资讯请关注我们网站的更新!江苏大学修订2017年博士、考研招生简章及专业目录的通知各学院;2017年是研究生招生政策改革调整之年,招生简章及专业目录修订工作启动较往年面临时间紧任务重的形势,根据实际情况,现将相关专业目录修订的相关要求通知如下:1、招生简章待国家公布最新政策后由研究生院负责制订发布,新增专业学..._江苏大学计算机专硕有哪些专业

随便推点

ionic cordova使用笔记_cordova-plugin-autostart-master-程序员宅基地

文章浏览阅读9k次。ioniccordovaionic typescript插件ionic cordova resources登录问题_cordova-plugin-autostart-master

Python Requests库简明使用教程_python requests 带query实例-程序员宅基地

文章浏览阅读7.6k次。Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了。它是为另一个时代、另一个互联网所创建的。它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务。发送GET请求import urllib.request f = urllib.request.urlopen('http://www.webxml.com.cn//webservices_python requests 带query实例

计算机网络系列笔记(三) - 传输层_一个协议可以同时出现在多个层吗?-程序员宅基地

文章浏览阅读1.3k次。传输层概述传输层的基本理论和基本机制: 复用/分用 , 可靠数据传输机制, 流量控制机制, 拥塞控制机制掌握Internet的传输层协议: UDP, TCP, TCP拥塞控制传输层协议为运行在不同host上的进程提供了一种逻辑通信机制网络层提供主机之间的逻辑通信机制, 传输层提供应用进程之间的逻辑通信机制, 位于网络层之上, 依赖于网络层服务, 对网络层服务进行增强TCP: 可靠, 按序的交付服务, 拥塞控制, 流量控制, 连接建立UDP: 不可靠的交付服务, 基于尽力而为的网络层, 没有做可_一个协议可以同时出现在多个层吗?

Python-Flask-migrate安装和使用_flask_migrate安装-程序员宅基地

文章浏览阅读3k次。在开发过程中,需要修改数据库模型,而且还要在修改之后更新数据库。最直接的方式就是删除旧表,但这样会丢失数据。更好的解决办法是使用数据库迁移框架,它可以追踪数据库模式的变化,然后把变动应用到数据库中。在Flask中可以使用Flask-Migrate扩展,来实现数据迁移。环境:window11+pycharm2020.1+Anaconda4.11.0 +python3.7Flask-sqlalchemy2.5.1Flask-Migrate:3.1.0......_flask_migrate安装

Java中内嵌groovy,JS脚本_groovy js-程序员宅基地

文章浏览阅读1.2k次。最近设计一个数据统计系统,系统中上百种数据统计维度,而且这些数据统计的指标可能随时会调整.如果基于java编码的方式逐个实现数据统计的API设计,工作量大而且维护起来成本较高;最终确定为将"数据统计"的计算部分单独分离成脚本文件(javascript,或者Groovy),非常便捷了实现了"数据统计Task" 与 "数据统计规则(计算)"解耦,且可以动态的加载和运行的能力.顺便对JAVA嵌入运行Groovy脚本做个备忘. Java中运行Groovy,有三种比较常用的类支持:GroovyShell,G.._groovy js

BASE64Encoder及BASE64Decoder 正确用法_spring base64encoder 用法-程序员宅基地

文章浏览阅读827次。一直以来Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Decoder类。这人个类是sun公司的内部方法,并没有在java api中公开过,不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用。但是在eclipse和MyEclipse中直接使用,却找不到该类。如下图!_spring base64encoder 用法