鱼C论坛

 找回密码
 立即注册
查看: 1106|回复: 0

[技术交流] 43讲动动手第1题 答案有几个小问题

[复制链接]
发表于 2017-3-29 22:49:41 | 显示全部楼层 |阅读模式

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

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

x
源答案代码如下:
  1. class Word(str):
  2. '''存储单词的类,定义比较单词的几种方法'''

  3.     def __new__(cls, word):
  4.         # 注意我们必须要用到 __new__ 方法,因为 str 是不可变类型
  5.         # 所以我们必须在创建的时候将它初始化
  6.         if ' ' in word:
  7.             print "Value contains spaces. Truncating to first space."
  8.             word = word[:word.index(' ')] #单词是第一个空格之前的所有字符
  9.         return str.__new__(cls, word)

  10.     def __gt__(self, other):
  11.         return len(self) > len(other)
  12.     def __lt__(self, other):
  13.         return len(self) < len(other)
  14.     def __ge__(self, other):
  15.         return len(self) >= len(other)
  16.     def __le__(self, other):
  17.         return len(self) <= len(other)
复制代码


问题1:
  1. '''存储单词的类,定义比较单词的几种方法'''
复制代码
应该有缩进
问题2:
  1. print "Value contains spaces. Truncating to first space."
复制代码
这是Python2的语法吧~~~
问题3:
  1. word = word[:word.index(' ')] #单词是第一个空格之前的所有字符
复制代码
  如果输入的单词首位就是空格呢~~~比如
  1. "     asdf    sdfsdfa"
复制代码


感觉这些小问题不像我鱼啊,我鱼一向不是非常严谨吗,我鱼加油,祝鱼C越来越好!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 23:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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