电脑

当前位置 /首页/游戏数码/电脑/列表

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

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

操作方法

(01)打开JUPTER NOTEBOOK,新建一个PY文档。

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

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

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

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

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

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

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

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

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

(06)class Retangle():def __init__(self, width, length):h = th = length我们定义一下新的类。

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

(07)def size(self):return h * thshape = Retangle(5, 6)print(shape)可以看到这样得不到我们要的结果。

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

(08)原因就是我们后面定义的size不能再后面定义,要在定义类的时候一起定义。

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

(09)只要分开都会出现问题。

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

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

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

特别提示

注意class的运用

TAG标签:python 矩形 计算 面积 #