鱼C论坛

 找回密码
 立即注册
查看: 2072|回复: 5

Function和list

[复制链接]
发表于 2019-9-24 19:42:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这个是《笨方法学习Python3》里的ex25,代码跟作者的代码对照是一样的。
前面输入的命令都是执行,到最后两个就这样报错了。
求帮忙看一下是哪里出问题了。

没报错的

没报错的

  1. def break_words(stuff):
  2.     """This function will break up words for us."""
  3.     words = stuff.split(' ')
  4.     return words

  5. def sort_words(words):
  6.     """Sorts the words."""
  7.     return sorted(words)

  8. def print_first_word(words):
  9.     """Prints the first word after popping it off."""
  10.     word = words.pop(0)
  11.     print(word)

  12. def print_last_word(words):
  13.     """Prints the last word after popping it off."""
  14.     word = words.pop(-1)
  15.     print(word)

  16. def sort_sentence(sentence):
  17.     """Takes in a full sentence and returns the sorted words."""
  18.     words = break_words(sentence)
  19.     return sort_words(words)

  20. def print_first_and_last(sentence):
  21.     """Prints the first and last words of the sentence."""
  22.     words = break_words(sentence)
  23.     print_first_word(words)
  24.     print_last_word(words)

  25. def print_first_and_last_sorted(sentence):
  26.     """Sorts the words then prints the first and last one."""
  27.     words = sort_sentence(sentence)
  28.     print_first_word(words)
  29.     print_last_word(words)
复制代码

报错的

报错的

作者的命令

作者的命令

作者的代码

作者的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-24 20:36:06 | 显示全部楼层
你可能什么时候把sentence变成列表了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-24 20:41:19 | 显示全部楼层
你贴出来那段英文,不是让你运行之后看看你发现了什么错误,然后让你跟着完善吗?

作者的意图就是让你发现出了错误,然后让你自己完善啊。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-24 20:50:05 | 显示全部楼层
风丶少 发表于 2019-9-24 20:41
你贴出来那段英文,不是让你运行之后看看你发现了什么错误,然后让你跟着完善吗?

作者的意图就是让你发 ...

是出错了,我来来回回看了试了几遍还是不知道为什么错,所以才发帖求助啊……
直接运行ex25.py没有报错…… 输入最后两个命令报错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-24 21:01:24 | 显示全部楼层
风丶少 发表于 2019-9-24 20:41
你贴出来那段英文,不是让你运行之后看看你发现了什么错误,然后让你跟着完善吗?

作者的意图就是让你发 ...

我找到为什么了……谢谢回复
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-24 21:02:24 | 显示全部楼层
塔利班 发表于 2019-9-24 20:36
你可能什么时候把sentence变成列表了

我找到为什么错了……谢谢回复
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-19 14:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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