【Python】Python3中的除法
程序员文章站
2022-04-22 12:17:34
【Python】Python3中的除法示例:import unittest # 导入unittest这个模块class Test(unittest.TestCase): # 定义一个测试的类,并继承unittest.TestCase这个类 def testMinus(self): # 定义测试case,要以test开头 result = 6-5 expect = 1 self.assertEqual(result, expect) #...
示例:
import unittest # 导入unittest这个模块
import math # 导入math模块
class Test(unittest.TestCase): # 定义一个测试的类,并继承unittest.TestCase这个类
def testMinus(self): # 定义测试case,要以test开头
result = 6-5 expect = 1 self.assertEqual(result, expect) # 断言,assertEqual,判断是否相等
def testdiv1(self): result = 7/2 # 精确除法,返回float expect = 3.5 self.assertEqual(result, expect) def testdiv2(self): result = 7//2 # 去尾取整,返回int expect = 3 self.assertEqual(result, expect) def testdiv3(self): result = math.ceil(7/2) # 向上取整
expect = 4 self.assertEqual(result, expect) def testdiv4(self): result = math.floor(7/2) # 向下取整
expect = 3 self.assertEqual(result, expect) def testdiv5(self): result = round(7/2) # 四舍五入取整
expect = 4 self.assertEqual(result, expect) if __name__ == '__main__': unittest.main() # 运行主函数
结果:
本文地址:https://blog.csdn.net/qq576677533/article/details/108255735
上一篇: 电脑名词的生活化解释