鱼C论坛

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

[已解决]python代码报错

[复制链接]
发表于 2021-12-17 14:57:14 | 显示全部楼层 |阅读模式

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

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

x

  1. class TemperaTure:
  2.     def __init__(self, hsd=0, ssd=0):
  3.         self.hsd = hsd
  4.         self.ssd = ssd
  5.         self.prompt = 0

  6.     def __str__(self):
  7.         return self.prompt

  8.     __repr__ = __str__

  9.    
  10.     def __setattr__(self, name, value):
  11.         if name == 'hsd':
  12.             self.__dict__[name] = value
  13.             self.__dict__['ssd'] = (value-32)/1.8
  14.             self.prompt = '华氏度为%0.2f\n摄氏度为%0.2f' % (self.hsd, self.ssd)
  15.         elif name == 'ssd':
  16.             self.__dict__[name] = value
  17.             self.__dict__['hsd'] = value*1.8+32
  18.             self.prompt = '摄氏度为%0.2f\n华氏度为%0.2f' % (self.ssd, self.hsd)
  19.         else:
  20.             return '请正确输入!'
  21.         
复制代码


我想知道为什么错,如何修改,请大佬赐教,不胜感激!
最佳答案
2021-12-17 15:56:06
  1. class Temperature:
  2.         def __init__(self, C = 0, F = 0, text = None):
  3.                 self.C = C
  4.                 self.F = F
  5.                 self.text = text
  6.        
  7.         def __str__(self):
  8.                 return self.text
  9.        
  10.         def __setattr__(self, name, value):
  11.                 if name == "F":
  12.                         self.__dict__[name] = value
  13.                         self.__dict__["C"] = (value - 32)/1.8
  14.                 elif name == "C":
  15.                         self.__dict__[name] = value
  16.                         self.__dict__["F"] = value*1.8 + 32
  17.                 else:
  18.                         print("请正确输入!")
  19.                 self.__dict__["text"] = "华氏度为: %0.2f\n摄氏度为: %0.2f"%(self.F, self.C)
  20.                
  21. t = Temperature()
  22. t.C = 37
  23. print(t)
复制代码
1.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-17 15:27:37 | 显示全部楼层
改成
self.__dict__['prompt'] =
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-17 15:56:06 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
  1. class Temperature:
  2.         def __init__(self, C = 0, F = 0, text = None):
  3.                 self.C = C
  4.                 self.F = F
  5.                 self.text = text
  6.        
  7.         def __str__(self):
  8.                 return self.text
  9.        
  10.         def __setattr__(self, name, value):
  11.                 if name == "F":
  12.                         self.__dict__[name] = value
  13.                         self.__dict__["C"] = (value - 32)/1.8
  14.                 elif name == "C":
  15.                         self.__dict__[name] = value
  16.                         self.__dict__["F"] = value*1.8 + 32
  17.                 else:
  18.                         print("请正确输入!")
  19.                 self.__dict__["text"] = "华氏度为: %0.2f\n摄氏度为: %0.2f"%(self.F, self.C)
  20.                
  21. t = Temperature()
  22. t.C = 37
  23. print(t)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-17 17:09:48 | 显示全部楼层
class Temperature:
        def __init__(self, C = 0, F = 0, text = None):
                self.C = C
                self.F = F
                self.text = text
        
        def __str__(self):
                return self.text
        
        def __setattr__(self, name, value):
                if name == "F":
                        self.__dict__[name] = value
                        self.__dict__["C"] = (value - 32)/1.8
                elif name == "C":
                        self.__dict__[name] = value
                        self.__dict__["F"] = value*1.8 + 32
                else:
                        print("请正确输入!")
                self.__dict__["text"] = "华氏度为: %0.2f\n摄氏度为: %0.2f"%(self.F, self.C)
               
t = Temperature()
t.C = 37
print(t)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-19 19:10:20 | 显示全部楼层
本帖最后由 sunyt 于 2021-12-19 19:15 编辑


我想知道为什么
  1. self.prompt = '摄氏度为%0.2f\n华氏度为%0.2f' % (self.ssd, self.hsd)
复制代码

会报错


  1. self.__dict__["text"] = "华氏度为: %0.2f\n摄氏度为: %0.2f"%(self.F, self.C)
复制代码

是正确的

__str__应该是可以返回字符串的啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-19 19:11:04 | 显示全部楼层
本帖最后由 sunyt 于 2021-12-19 19:14 编辑

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-19 19:32:38 | 显示全部楼层
sunyt 发表于 2021-12-19 19:10
我想知道为什么

会报错

你的报错和 __str__ 没有任何关系,主要是 __setattr__() 你先要了解其用法。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 14:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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