鱼C论坛

 找回密码
 立即注册
查看: 2252|回复: 3

[已解决]为什么我这个是错的呀

[复制链接]
发表于 2023-9-13 20:26:32 | 显示全部楼层 |阅读模式

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

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

x
class Rectangle:
    def __init__(self,length,width):
        self.length=length
        self.width=width
    def set_length(self):
        self.set_length=input("请输入长方形的长度:")
    def set_width(self):
        self.set_width=input("请输入长方形的宽度")
    def get_perimeter(self):
        if self.length and self.width == True:
            self.get_perimeter=2*(self.length+self.width)
            return self.get_perimeter
        else:
            print('长或宽未设置')
    def get_area(self):
        if self.length and self.width == True:
            self.get_area=self.length*self.width
            return self.get_area
        else:
            print('长或宽未设置')
A=Rectangle(1,2)
print(A.get_area)

运行出来是这个
C:\Users\Yu\PycharmProjects\python_learning\venv\Scripts\python.exe C:\Users\Yu\PycharmProjects\python_learning\venv\学生类.py
<bound method Rectangle.get_area of <__main__.Rectangle object at 0x0000018C308E5B50>>

Process finished with exit code 0

最佳答案
2023-9-13 20:26:46
这是因为在调用  get_area  方法时,你没有加上括号,导致它返回的是一个方法对象而不是方法的返回值。你需要在  print(A.get_area)  后面加上括号,即  print(A.get_area()) ,这样才能正确地输出长方形的面积。同时,在  set_length  和  set_width  方法中,你应该将输入的值赋给  self.length  和  self.width ,而不是将输入函数名赋给它们。正确的代码如下所示:

  1. class Rectangle:
  2.     def __init__(self,length,width):
  3.         self.length=length
  4.         self.width=width
  5.     def set_length(self):
  6.         self.length=float(input("请输入长方形的长度:"))
  7.     def set_width(self):
  8.         self.width=float(input("请输入长方形的宽度"))
  9.     def get_perimeter(self):
  10.         if self.length and self.width:
  11.             self.get_perimeter=2*(self.length+self.width)
  12.             return self.get_perimeter
  13.         else:
  14.             print('长或宽未设置')
  15.     def get_area(self):
  16.         if self.length and self.width:
  17.             self.get_area=self.length*self.width
  18.             return self.get_area
  19.         else:
  20.             print('长或宽未设置')
  21.             
  22. A=Rectangle(1,2)
  23. print(A.get_area())
复制代码
屏幕截图 2023-09-13 202521.png

本帖被以下淘专辑推荐:

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

使用道具 举报

发表于 2023-9-13 20:26:46 | 显示全部楼层    本楼为最佳答案   
这是因为在调用  get_area  方法时,你没有加上括号,导致它返回的是一个方法对象而不是方法的返回值。你需要在  print(A.get_area)  后面加上括号,即  print(A.get_area()) ,这样才能正确地输出长方形的面积。同时,在  set_length  和  set_width  方法中,你应该将输入的值赋给  self.length  和  self.width ,而不是将输入函数名赋给它们。正确的代码如下所示:

  1. class Rectangle:
  2.     def __init__(self,length,width):
  3.         self.length=length
  4.         self.width=width
  5.     def set_length(self):
  6.         self.length=float(input("请输入长方形的长度:"))
  7.     def set_width(self):
  8.         self.width=float(input("请输入长方形的宽度"))
  9.     def get_perimeter(self):
  10.         if self.length and self.width:
  11.             self.get_perimeter=2*(self.length+self.width)
  12.             return self.get_perimeter
  13.         else:
  14.             print('长或宽未设置')
  15.     def get_area(self):
  16.         if self.length and self.width:
  17.             self.get_area=self.length*self.width
  18.             return self.get_area
  19.         else:
  20.             print('长或宽未设置')
  21.             
  22. A=Rectangle(1,2)
  23. print(A.get_area())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-9-13 20:28:56 | 显示全部楼层
isdkz 发表于 2023-9-13 20:26
这是因为在调用  get_area  方法时,你没有加上括号,导致它返回的是一个方法对象而不是方法的返回值。你需 ...

哇 太感谢你了 你是机器人吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-13 21:52:31 | 显示全部楼层
Gigi233 发表于 2023-9-13 20:28
哇 太感谢你了 你是机器人吗

脚本+GPT
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 17:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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