当前位置: 首页>>代码示例>>Python>>正文


Python Calculator.divide方法代码示例

本文整理汇总了Python中calculator.Calculator.divide方法的典型用法代码示例。如果您正苦于以下问题:Python Calculator.divide方法的具体用法?Python Calculator.divide怎么用?Python Calculator.divide使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在calculator.Calculator的用法示例。


在下文中一共展示了Calculator.divide方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TddInPythonExample

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
class TddInPythonExample(unittest.TestCase):
    def setUp(self):
        self.calc = Calculator()

    def test_calculator_add_method_returns_correct_result(self):
        result = self.calc.add(2, 2)
        self.assertEqual(4, result)

    def test_calculator_returns_error_message_if_both_args_not_numbers(self):
        self.assertRaises(ValueError, self.calc.add, 'two', 'three')
    
    def test_calculator_subtract_method_returns_correct_result(self):
	result = self.calc.subtract(4,2)
	self.assertEqual(2, result)
    
    def test_calculator_returns_error_message_if_both_args_not_numbers(self):
	self.assertRaises(ValueError, self.calc.subtract, "four", "two")
    def test_calculator_multiply_method_returns_correct_result(self):
	result = self.calc.multiply(3,5)
	self.assertEqual(15, result)
    def test_calculator_returns_error_message_if_both_args_not_number(self):
	self.assertRaises(ValueError, self.calc.multiply, "three","five")
    def test_calculator_divide_method_returns_correct_result(self):
	result = self.calc.divide(15,3)
	self.assertEqual(5, result)
    def test_calcualtor_returns_error_message_if_btoh_args_not_number(self):
	self.assertRaises(ValueError, self.calc.divide, "fifteen","three")
开发者ID:rdong95,项目名称:NewProject,代码行数:29,代码来源:test_calculator.py

示例2: TestCalculator

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
class TestCalculator(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        # Create an instance of the calculator that can be used in all tests
        self.calc = Calculator()
        print('Set up class')

    @classmethod
    def tearDownClass(self):
        print('Tear down class')

    def test_add(self):
        self.assertEqual(self.calc.add(2, 7), 9)

    # Write test methods for subtract, multiply, and divide

    def test_subtract(self):
        self.assertEqual(self.calc.subtract(5, 3), 2)

    def test_multiply(self):
        self.assertEqual(self.calc.multiply(2, 7), 14)

    def test_divide(self):
        self.assertEqual(self.calc.divide(14, 7), 2)
开发者ID:MeganCFord,项目名称:NSS-PY1-testedCalculator,代码行数:27,代码来源:CalcTest.py

示例3: CalculatorTest

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
class CalculatorTest(TestCase):
    def setUp(self):
        self.calc = Calculator()

    operations = lambda: ((6, 3, 9), (-8, 4, -4), (4, -3, 1), (-7, -8, -15), (2.7, 3.56, 6.26), (-4.44, 6.759, 2.319),
                          (5.53, -7.68, -2.15), (-24.9, -6.53, -31.43), (0, -3.43, -3.43), (0, 0, 0), (4.7, 0, 4.7))

    @data_provider(operations)
    def test_add(self, a, b, ans):
        self.assertEqual(ans, round(self.calc.add(a, b), 3))

    operations = lambda: ((6, 3, 3), (-8, 4, -12), (4, -3, 7), (-7, -8, 1), (2.7, 3.56, -0.86), (-4.44, 6.759, -11.199),
                          (5.53, -7.68, 13.21), (-24.9, -6.53, -18.37), (0, -3.43, 3.43), (0, 0, 0), (4.7, 0, 4.7))

    @data_provider(operations)
    def test_subtract(self, a, b, ans):
            self.assertEqual(ans, round(self.calc.subtract(a, b), 3))

    operations = lambda: ((6, 3, 18), (-8, 4, -32), (4, -3, -12), (-7, -8, 56), (2.7, 3.56, 9.612), (-4.44, 6.759, -30.01),
                          (5.53, -7.68, -42.47), (-24.9, -6.53, 162.597), (0, -3.43, -0), (0, 0, 0), (4.7, 0, 0))

    @data_provider(operations)
    def test_multiply(self, a, b, ans):
            self.assertEqual(ans, round(self.calc.multiply(a, b), 3))

    operations = lambda: ((6, 3, 2), (-8, 4, -2), (4, -3, -1.333), (-7, -8, 0.875), (2.7, 3.56, 0.758), (-4.44, 6.759, -0.657),
                          (5.53, -7.68, -0.72), (-24.9, -6.53, 3.813), (0, -3.43, -0))

    @data_provider(operations)
    def test_divide(self, a, b, ans):
        self.assertEqual(ans, round(self.calc.divide(a, b), 3))
        with self.assertRaises(ZeroDivisionError):
            self.calc.divide(4.7, 0)

    def test_evaluate(self):
        self.assertEqual(-6, self.calc.evaluate("3+(4-7)*3"))
        self.assertEqual(17, self.calc.evaluate("2.5*3.2+(4.7-3.2)*6"))
        with self.assertRaises(ZeroDivisionError):
            self.calc.evaluate("2/0")
开发者ID:alex0912nsk,项目名称:lesson_3_homework,代码行数:41,代码来源:calculator_test.py

示例4: CalculatorTest

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
class CalculatorTest(unittest.TestCase):
    def setUp(self):
        self.calc = Calculator()

    def test_add(self):
        result = self.calc.add(4, 8)
        self.assertEqual(12, result)

    def test_subtract(self):
        result = self.calc.subtract(8, 4)
        self.assertEqual(4, result)

    def test_multiply(self):
        result = self.calc.multiply(8, 4)
        self.assertEqual(32, result)

    def test_divide(self):
        result = self.calc.divide(8, 4)
        self.assertEqual(2, result)
开发者ID:lizcarey13,项目名称:hackbright-1,代码行数:21,代码来源:test_calculator.py

示例5: test_divide

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
def test_divide():
    myCalculator = Calculator()
    assert_equal(myCalculator.divide(8, 4), 2)
开发者ID:sophialsanchez,项目名称:calculator,代码行数:5,代码来源:tests.py

示例6: test_divide_10_entre_2

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
 def test_divide_10_entre_2(self):
     cal = Calculator()
     self.assertEqual(5, cal.divide(10, 2))
开发者ID:miguelvicedo8,项目名称:Calculadora,代码行数:5,代码来源:TestCalculadora.py

示例7: test_divide_5_entre_1

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
 def test_divide_5_entre_1(self):
     cal = Calculator()
     self.assertEqual(5, cal.divide(5, 1))
开发者ID:miguelvicedo8,项目名称:Calculadora,代码行数:5,代码来源:TestCalculadora.py

示例8: test_divide_6_entre_3

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
 def test_divide_6_entre_3(self):
     cal = Calculator()
     self.assertEqual(2, cal.divide(6, 3))
开发者ID:miguelvicedo8,项目名称:Calculadora,代码行数:5,代码来源:TestCalculadora.py

示例9: CalculatorTestCase

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
class CalculatorTestCase(unittest.TestCase):

    def setUp(self):
        self.calculator = Calculator()

#	add       

    def testAdd(self):
        self.assertEqual(10, self.calculator.add(3, 7))

    def testAddNegative(self):
        self.assertEqual(0, self.calculator.add(10, -10))

    def testAddDecimal(self):
        self.assertEqual(1, self.calculator.add(.5, .5))

    def testAddStringWithNumber(self):
        self.assertEqual(3, self.calculator.add('1', 2))

    def testAddExp(self):
        self.assertEqual(10.2, self.calculator.add('1e1', .2))

    def testAddRandomString(self):
        self.assertEqual('error', self.calculator.add('1e1asfasf', .2))

#	subtract

    def testSubtract(self):
        self.assertEqual(0, self.calculator.subtract(10, 10))

    def testSubtractNegative(self):
        self.assertEqual(-17, self.calculator.subtract(-3, 14))

    def testSubtractDecimal(self):
        self.assertEqual(.4, self.calculator.subtract(.7, .3))

    def testSubtractStringWithNumber(self):
        self.assertEqual(10, self.calculator.subtract('13', 3))

    def testSubtractExp(self):
        self.assertEqual(0, self.calculator.subtract('1e1', 10))

    def testSubtractRandomString(self):
        self.assertEqual('error', self.calculator.subtract('13lasfkllklaskf', 3))

#	multiply        

    def testMultiply(self):
        self.assertEquals(0, self.calculator.multiply(5, 0))

    def testMultiplyNegative(self):
        self.assertEquals(-12, self.calculator.multiply(3, -4))

    def testMultiplyDecimal(self):
        self.assertEquals(1, self.calculator.multiply(.1, 10))

    def testMultiplyStringWithNumber(self):
        self.assertEquals(30, self.calculator.multiply('10', 3))

    def testMultiplyExp(self):
        self.assertEquals(1e3, self.calculator.multiply(1e2, 10))

    def testMultiplyRandomString(self):
        self.assertEquals('error', self.calculator.multiply('hi', 'there'))

#	divide

    def testDivide(self):
        self.assertEquals(5, self.calculator.divide(10, 2))

    def testDivideNull(self):
        self.assertRaises(ValueError, self.calculator.divide, 3, 0)

    def testDivideNegative(self):
        self.assertEquals(4, self.calculator.divide(-24, -6))

    def testDivideDecimal(self):
        self.assertEquals(100, self.calculator.divide(10, .1))

    def testDivideStringWithNumber(self):
        self.assertEquals(25, self.calculator.divide('100', 4))

    def testDivideExp(self):
        self.assertEquals(10, self.calculator.divide(1e2, 10))

    def testDivideRandomString(self):
        self.assertEquals('error', self.calculator.divide('hi', 'there'))

#	cos

    def testCosZero(self):
        self.assertEqual(1, self.calculator.cos(0))

    def testCosPi(self):
        self.assertEqual(-1, self.calculator.cos(math.pi))

    def testCosRange(self):
    	value = self.calculator.cos(1)
        self.assertLess(0, value)
        self.assertGreater(1, value)
开发者ID:Fatkhi,项目名称:home-assignment-3,代码行数:102,代码来源:test_calculator.py

示例10: print

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
            n = i
            op = Exp[i]
            break
    if n == 0:
        print('No sign of the operation')
        continue
    try:
        a = float(Exp[:n])
        b = float(Exp[n+1:])
    except ValueError:
        print('wrongly entered value')
        continue

    if op == '+':
        ans = calc.add(a, b)
    elif op == '-':
        ans = calc.subtract(a, b)
    elif op == '*':
        ans = calc.multiply(a, b)
    elif op == ':' or op == '/':
        if b == 0:
            print('cant zero division')
            continue
        ans = calc.divide(a, b)
    if op == ':':
        Exp = str(a) + "/" + str(b)
    if ans == calc.evaluate(Exp):
        print(Exp + "=" + str(ans))
    else:
        print('Sorry, we cant find right answer')
开发者ID:alex0912nsk,项目名称:lesson_3_homework,代码行数:32,代码来源:main.py

示例11: CalculatorTestCase

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
class CalculatorTestCase(unittest.TestCase):
    def setUp(self):
        self.calculator = Calculator()

    """Add tests"""

    def test_add_of_ints(self):
        self.assertEqual(self.calculator.add(5, 1), 6)

    def test_commutativity_of_add_of_ints(self):
        self.assertEqual(self.calculator.add(1, 5), self.calculator.add(5, 1))

    def test_add_of_int_and_float(self):
        self.assertEqual(self.calculator.add(5, 3.22), 8.22)

    def test_commutativity_of_add_of_int_and_float(self):
        self.assertEqual(self.calculator.add(3.2, 5), self.calculator.add(5, 3.2))

    def test_add_of_floats(self):
        self.assertEqual(self.calculator.add(0.1, 0.2), 0.3)

    def test_commutativity_of_add_of_floats(self):
        self.assertEqual(self.calculator.add(1.2, 5.23), self.calculator.add(5.23, 1.2))

    def test_negative_add(self):
        self.assertNotEqual(self.calculator.add(3, 4), -7)

    """Subtract tests"""

    def test_subtract_of_ints(self):
        self.assertEqual(self.calculator.subtract(1, 5), -4)

    def test_subtract_of_int_and_float(self):
        self.assertEqual(self.calculator.subtract(5, 3.22), 1.78)

    def test_subtract_of_floats(self):
        self.assertEqual(self.calculator.subtract(5.3, 1.2), 4.1)

    def test_negative_subtract(self):
        self.assertNotEqual(self.calculator.subtract(4, 3), -1)

    """Multiply tests"""

    def test_multiply_of_ints(self):
        self.assertEqual(self.calculator.multiply(5, 2), 10)

    def test_commutativity_of_multiply_of_ints(self):
        self.assertEqual(self.calculator.multiply(2, 5), self.calculator.multiply(5, 2))

    def test_multiply_of_int_and_float(self):
        self.assertEqual(self.calculator.multiply(12, 0.1), 1.2)

    def test_commutativity_of_multiply_of_int_and_float(self):
        self.assertEqual(self.calculator.multiply(3.2, 5), self.calculator.multiply(5, 3.2))

    def test_multiply_of_floats(self):
        self.assertEqual(self.calculator.multiply(5.2, 1.2), 6.24)

    def test_commutativity_of_multiply_of_floats(self):
        self.assertEqual(self.calculator.multiply(1.2, 5.23), self.calculator.multiply(5.23, 1.2))

    def test_negative_multiply(self):
        self.assertNotEqual(self.calculator.multiply(3, 4), -7)

    """Divide tests"""

    def test_divide_of_ints(self):
        self.assertEqual(self.calculator.divide(5, -2), -2.5)

    def test_divide_of_int_and_float(self):
        self.assertEqual(self.calculator.divide(5, 3.2), 1.5625)

    def test_divide_of_floats(self):
        self.assertEqual(self.calculator.divide(0.3, 0.1), 3)

    def test_divide_by_zero(self):
        self.assertRaises(ZeroDivisionError, self.calculator.divide, 3, 0)

    def test_negative_divide(self):
        self.assertNotEqual(self.calculator.divide(3, 4), -7)

    """Sin tests"""

    def test_sin_of_zero(self):
        self.assertEqual(self.calculator.sin(0), 0)

    def test_sin_of_pi_div_2(self):
        self.assertEqual(self.calculator.sin(math.pi / 2), 1)

    def test_sin_range(self):
        result = self.calculator.sin(5.4)
        self.assertLessEqual(result, 1)
        self.assertGreaterEqual(result, -1)

    def test_negative_sin(self):
        self.assertNotEqual(self.calculator.sin(math.pi / 6), 1)
开发者ID:KhasanovBI,项目名称:home-assignment-3,代码行数:98,代码来源:test_calculator.py

示例12: when_se_realiza_la_division

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
def when_se_realiza_la_division(step):
    calc = Calculator()
    world.resultado = calc.divide(world.num1, world.num2)
开发者ID:miguelvicedo8,项目名称:Calculadora,代码行数:5,代码来源:steps.py

示例13: test_divide

# 需要导入模块: from calculator import Calculator [as 别名]
# 或者: from calculator.Calculator import divide [as 别名]
	def test_divide(self):
		calculator = Calculator()
		result = calculator.divide(2,6)
		self.assertEqual(3, result)
开发者ID:EmilyK,项目名称:sample,代码行数:6,代码来源:calc_test.py


注:本文中的calculator.Calculator.divide方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。