Ejemplo n.º 1
0
def hypot(a, b):
    """Calculate and return the length of the hypotenuse of a right triangle.
    Do not use any functions other than those that are imported from your
    'calculator' module.

    Parameters:
        a: the length one of the sides of the triangle.
        b: the length the other non-hypotenuse side of the triangle.
    Returns:
        The length of the triangle's hypotenuse.
    """
    return calc.math.sqrt(calc.sum(calc.product(a, a), calc.product(b, b)))
    raise NotImplementedError("Problem 3 Incomplete")
def hypot(a, b):
    """Calculate and return the length of the hypotenuse of a right triangle.
    Do not use any functions other than those that are imported from your
    'calculator' module.

    Parameters:
        a: the length one of the sides of the triangle.
        b: the length the other non-hypotenuse side of the triangle.
    Returns:
        The length of the triangle's hypotenuse.
    """
    import calculator as cal
    return cal.math.sqrt(cal.sum(cal.product(a, a), cal.product(b, b)))
Ejemplo n.º 3
0
def prob3(a,b):
    """Calculate and return the length of the hypotenuse of a right triangle.
    Do not use any methods other than those that are imported from the
    'calculator' module.
    
    Parameters:
        a (float): the length one of the sides of the triangle.
        b (float): the length the other nonhypotenuse side of the triangle.
    
    Returns:
        The length of the triangle's hypotenuse.
    """
    import calculator

    leg_1 = calculator.product(a,a)
    leg_2 = calculator.product(b,b)
    sum_squared = calculator.sum(leg_1,leg_2)
    hypotenuse = calculator.square_root(sum_squared)
    return hypotenuse
Ejemplo n.º 4
0
from calculator import total, product, difference

print("The sum is :", total(2, 3))
print("The product is:", product(2, 3))
print("The difference is :", difference(2, 3))
Ejemplo n.º 5
0
def triangle(a, b):
    return calculator.Sqrt(
        calculator.add(calculator.product(a, a), calculator.product(b, b)))
Ejemplo n.º 6
0
from calculator import sum

print(sum(3, 5))
8
만약 모듈에 정의된 모든 변수, 함수, 클래스를 호출하려면 어떻게 할까요?

from calculator import sum, difference, product, square
위와 같은 방식으로 호출할 수도 있습니다만, 모듈에서 쓰고 싶은 변수, 함수, 클래스가 100개 이상이라면 그것들을 일일이 입력하는 것은 바보 같은 일이겠죠?

*를 쓰면, 모듈 안에 정의된 모든 변수/함수/클래스를 불러올 수 있습니다.

from calculator import *

print(sum(3, 5))
print(difference(3, 5))
print(product(3, 5))
print(square(3))
8
-2
15
9
그러나 모듈 파일의 일부만 사용하려고 할 때, *를 쓰는 것은 좋지 않은 습관(practice)입니다. 100개의 함수가 정의되어 있는데 4개만 사용하려고 한다면, *를 이용하기보다 하나 하나 직접 호출하는게 낫습니다. 스스로 상황 별로 적절히 잘 판단하여, 하나하나 직접 호출할지, 아니면 모듈에 정의된 모든 것을 호출할지 결정해야 합니다.

randint 함수와 uniform 함수
randint는 두 정수 사이의 어떤 랜덤한 정수(난수)를 리턴시켜주는 함수입니다. randint는 파이썬에 기본적으로 깔려있는 random이라는 모듈에 정의되어 있습니다. 따라서 이 함수를 사용하기 위해서는 random이라는 모듈로부터 불러와야 합니다.

from random import randint
함수를 불러오는 방법에 대해 봤으니, 이제 사용하는 방법에 대해 알아보겠습니다.

# a <= N <= b인, 랜덤한 정수(난수) N의 값을 리턴시켜준다.
randint(a, b)
Ejemplo n.º 7
0
import calculator

x = 22
y = 11

print("sum of {} and {} = {}".format(x, y, calculator.add(x, y)))

print("difference between {} and {} = {}".format(x, y, calculator.diff(x, y)))

print("product of {} and {} = {}".format(x, y, calculator.product(x, y)))

print("{} / {} = {}".format(x, y, calculator.divide(x, y)))
Ejemplo n.º 8
0
def problem_2_3(x,y):
    #Call on a method in calculator.py, which imports the math library
    sum = calc.sum(x,y)
    product = calc.product(x,y)
    return print("The sum is {}, and the product is {}".format(sum, product))
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 31 19:33:07 2020

@author: oleksii
"""

import calculator

print(calculator.summ(10, 5))
print(calculator.diff(30, 5))
print(calculator.product(2, 5))
print(calculator.fraction(25, 5))
print("-" * 10)

from calculator import summ, diff
print(summ(5, 5))
print(diff(5, 5))
print("-" * 10)

from calculator import summ, diff, product, fraction
# or
#from calculator import *

print(summ(5, 5))
print(diff(5, 5))
print(product(5, 5))
print(fraction(5, 5))
print("-" * 10)