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

 时间:2024-10-13 08:14:15

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

10、clas衡痕贤伎s Retangle(): def __init__(self, width, length): self.width 租涫疼迟= width self.length = length def size(self): return self.width * self.length在同一个方格制作就可以得到结果了。

如何用PYTHON计算出矩形的面积
  • Python编程:怎么计算100到200的所有素数
  • python统计字符串中字母数字出现的次数
  • python怎么保存
  • python素数判断代码
  • 使用python语言求1+2+3+...+100的和
  • 热门搜索
    小学生元宵节手抄报 父亲节手抄报图片 教师节的手抄报 绿色环保手抄报图片 有关绿色环保的手抄报 抗击病毒手抄报内容 创城手抄报 防病毒手抄报大全 重阳节手抄报简单易画 新型冠状病毒手抄报内容