专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

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

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/3/9 3:46:03       共计:3618 浏览

如何用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在同一个方格制作就可以得到结果了。

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:集合元素各数之和怎么算_java | ·下一条:在潍坊做java程序员待遇怎么样_java

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有