最新公告
  • 欢迎您光临知事网软件APP资源下载站,一个优质的手机App应用商店和网站源码基地。欢迎加入永久SVIP
  • 如何用python3输出分数?

    正文概述 知事网   2020-11-12 09:11:14   287

    上学的时候,数学是一批小伙伴们的噩梦,相比较语文的死记硬背方面,真的是读题一分钟,解题一小时。小编当年也被数学困住了,当然现在肯定是要改变这种情况。学习了python后,小编就找到了很多能够解决数学方面的技巧。今天就分数这个问题,小编把自己所学到的内容分享给大家。

    尝试编写一段计算直线斜率的代码,正在使用3.6。

     y1 = float(input("First y point: "))
      y2 = float(input("Second y point: "))
      x1 = float(input("First X point: "))
      x2 = float(input("Second X point: "))
     
      slope = (y2 - y1)/(x2 - x1)
     
      print("The slope is:",slope)

    每当输入使答案不合理的数字时,答案就会变成十进制。是否可以将其保留为零呢?

    解决:

    from fractions import Fraction
     
    y1 = int(input("First y point: "))
    y2 = int(input("Second y point: "))
    x1 = int(input("First X point: "))
    x2 = int(input("Second X point: "))
     
    slope = Fraction(y2 - y1, x2 - x1)
     
    print("The slope is:", slope, "=", float(slope))

    输入和输出:

    First y point: 5
    Second y point: 7
    First X point: 10
    Second X point: 15
    The slope is: 2/5 = 0.4

    以上就是小编解决分数问题的一个方法啦,相信有了今天的学习,小伙伴们应该不会对数学再感到害怕了。以后有很多的实用数学编程技巧,小编也会分享给大家的~


    知事网 » 如何用python3输出分数?

    发表评论

    还没有评论,快来抢沙发吧!

    [!--temp.right--]
    请选择支付方式
    ×
    余额支付
    ×
    微信扫码支付 0 元