首页后端开发Python如何用PYTHON计算出矩形的面积

如何用PYTHON计算出矩形的面积

时间2023-06-08 20:48:01发布访客分类Python浏览177
导读:如何用PYTHON计算出矩形的面积?用PYTHON计算出矩形的面积的方法和操作步骤如下:1、打开JUPTER NOTEBOOK,新建一个PY文档;2、width = 5length = 6size = width * lengthprint...

如何用PYTHON计算出矩形的面积?

用PYTHON计算出矩形的面积的方法和操作步骤如下:

1、打开JUPTER NOTEBOOK,新建一个PY文档;

2、width = 5length = 6size = width * lengthprint(size)定义三个变量,然后直接打印最后一个变量,这是一种方法;

3、width = input("Please input the width: ")length = input("Please input the width: ")size = width * lengthprint(size)也可以用INPUT,但是要注意这样会出现问题的;

4、width = int(input("Please input the width: "))length = int(input("Please input the width: "))size = width * lengthprint(size)字符串是不能做运算的,但是整型是可以的;

5、width = float(input("Please input the width: "))length = float(input("Please input the width: "))size = width * lengthprint(size)如果有小数点的话那么就要用浮点型了;

6、class Retangle(): def __init__(self, width, length): self.width = width self.length = length 我们定义一下新的类;

7、def size(self): return self.width * self.lengthshape = Retangle(5, 6)print(shape)可以看到这样得不到我们要的结果;

8、原因就是我们后面定义的size不能在后面定义,要在定义类的时候一起定义;

9、只要分开都会出现问题;

10、class Retangle(): def __init__(self, width, length): self.width = width self.length = length def size(self): return self.width * self.length在同一个方格制作就可以得到结果了。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 如何用PYTHON计算出矩形的面积
本文地址: https://pptw.com/jishu/67341.html
电脑没有python环境怎么设置 python简单编程题与7无关的数

游客 回复需填写必要信息