鱼C论坛

 找回密码
 立即注册
查看: 1332|回复: 6

[已解决]《零基础学习44:简单定制》遇到问题

[复制链接]
发表于 2019-11-14 14:56:21 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 rome99320 于 2019-11-14 14:57 编辑
  1. import time as t

  2. class MyTimer():
  3.      def __str__(self):
  4.         return self.prompt

  5.     __repr__ = __str__
  6.    
  7.     def start(self):
  8.         self.begin = t.localtime()
  9.         print ('开始计时了...')

  10.     def stop(self):
  11.         self.end = t.localtime()
  12.         self._calc()
  13.         print ('计时停止了!')

  14.     def _calc(self):
  15.         self.lasted=[]
  16.         self.prompt = '共运行了'
  17.         for index in range(6):
  18.             self.lasted.append(self.end[index]- self.begin[index])
  19.             self.prompt += str(self.lasted[index])

  20.         print (self.prompt)
复制代码


在运行上述代码的时候,系统指出“__repr__ = __str__”为“unindient does not match any outer indentation level”
请问要怎么解决,谢谢
最佳答案
2019-11-14 15:05:56
本帖最后由 Jery_wang09 于 2019-11-14 15:12 编辑
  1. import time as t

  2. class MyTimer():
  3.      def __str__(self):    ###这里缩进有问题,多缩进了一个空格
  4.         return self.prompt

  5.     __repr__ = __str__
  6.    
  7.     def start(self):
  8.         self.begin = t.localtime()
  9.         print ('开始计时了...')

  10.     def stop(self):
  11.         self.end = t.localtime()
  12.         self._calc()
  13.         print ('计时停止了!')

  14.     def _calc(self):
  15.         self.lasted=[]
  16.         self.prompt = '共运行了'
  17.         for index in range(6):
  18.             self.lasted.append(self.end[index]- self.begin[index])
  19.             self.prompt += str(self.lasted[index])

  20.         print (self.prompt)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-11-14 15:05:56 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Jery_wang09 于 2019-11-14 15:12 编辑
  1. import time as t

  2. class MyTimer():
  3.      def __str__(self):    ###这里缩进有问题,多缩进了一个空格
  4.         return self.prompt

  5.     __repr__ = __str__
  6.    
  7.     def start(self):
  8.         self.begin = t.localtime()
  9.         print ('开始计时了...')

  10.     def stop(self):
  11.         self.end = t.localtime()
  12.         self._calc()
  13.         print ('计时停止了!')

  14.     def _calc(self):
  15.         self.lasted=[]
  16.         self.prompt = '共运行了'
  17.         for index in range(6):
  18.             self.lasted.append(self.end[index]- self.begin[index])
  19.             self.prompt += str(self.lasted[index])

  20.         print (self.prompt)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-14 15:53:57 | 显示全部楼层
本帖最后由 jackz007 于 2019-11-14 15:55 编辑

        这一句
  1.      def __str__(self):
复制代码

        的缩进位置不对,前面应该是 4 个空格,可你这里是 5 个,只要删除掉 1 个就好了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-14 18:36:44 | 显示全部楼层
def __str__(self) 这一句缩进不对,一眼就能看出来。

  1. import time as t

  2. class MyTimer():
  3.     def __str__(self):
  4.         return self.prompt

  5.     __repr__ = __str__
  6.    
  7.     def start(self):
  8.         self.begin = t.localtime()
  9.         print ('开始计时了...')

  10.     def stop(self):
  11.         self.end = t.localtime()
  12.         self._calc()
  13.         print ('计时停止了!')

  14.     def _calc(self):
  15.         self.lasted=[]
  16.         self.prompt = '共运行了'
  17.         for index in range(6):
  18.             self.lasted.append(self.end[index]- self.begin[index])
  19.             self.prompt += str(self.lasted[index])

  20.         print (self.prompt)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-15 09:58:23 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-15 10:00:14 | 显示全部楼层
zltzlt 发表于 2019-11-14 18:36
def __str__(self) 这一句缩进不对,一眼就能看出来。

非常感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-15 10:02:25 | 显示全部楼层
jackz007 发表于 2019-11-14 15:53
这一句

        的缩进位置不对,前面应该是 4 个空格,可你这里是 5 个,只要删除掉 1 个就好 ...

非常感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 13:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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