广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

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

    来源:网络收集  点击:  时间:2024-04-18
    【导读】:
    如何用PYTHON计算出矩形的面积工具/原料morePYTHON方法/步骤1/10分步阅读

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

    2/10

    width = 5

    length = 6

    size = width * length

    print(size)

    定义三个变量,然后直接打印最后一个变量,这是一种方法。

    3/10

    width = input(Please input the width: )

    length = input(Please input the width: )

    size = width * length

    print(size)

    也可以用INPUT,但是要注意这样会出现问题的。

    4/10

    width = int(input(Please input the width: ))

    length = int(input(Please input the width: ))

    size = width * length

    print(size)

    字符串是不能做运算的,但是整型是可以的。

    5/10

    width = float(input(Please input the width: ))

    length = float(input(Please input the width: ))

    size = width * length

    print(size)

    如果有小数点的话那么就要用浮点型了。

    6/10

    class Retangle():

    def __init__(self, width, length):

    self.width = width

    self.length = length

    我们定义一下新的类。

    7/10

    def size(self):

    return self.width * self.length

    shape = Retangle(5, 6)

    print(shape)

    可以看到这样得不到我们要的结果。

    8/10

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

    9/10

    只要分开都会出现问题。

    10/10

    class Retangle():

    def __init__(self, width, length):

    self.width = width

    self.length = length

    def size(self):

    return self.width * self.length

    在同一个方格制作就可以得到结果了。

    注意事项

    注意class的运用

    PYTHON矩形面积
    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_541780.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-05 05:52:51  耗时:0.026
    0.0264s