DigitalNomad

계산기 본문

파이썬으로 수학 풀기

계산기

훌루루훌루 2017. 12. 10. 20:39

연산자와 숫자 2개 입력받고 결과값 출력하자

반복할 지 물어보자

함수구성


def welcome() # 시작 문구


def calculator() # 계산기 - 연산자와 숫자 2개 입력받고 결과값 출력  


def again() # 한번 더 할지 물어보자


if __name__ = '__main__': # 메인함수

코드


def welcome(): # 시작 문구

    print('''

Welcome to Calculator

''')



def calculator():

    # 순서대로 덧셈, 뺄셈, 곱셈, 나눗셈, 지수, 나머지 순이다

    operator = input('''

Please type in the math operation you would like to complete:

+ for addition  

- for subtraction

* for multiplication

/ for division

** for power

% for modulo

Please enter the operator: 

''')

    # 입력받는대로 실수로 바꾼다

    num1 = float(input('Please enter the first number: '))

    num2 = float(input('Please enter the second number: '))



    # 계산하자

    if operator == '+':

        print('{} + {} = {}'.format(num1, num2, num1 + num2))

    elif operator == '-':

        print('{} - {} = {}'.format(num1, num2, num1 - num2))

    elif operator == '*':

        print('{} * {} = {}'.format(num1, num2, num1 * num2))

    elif operator == '/':

        print('{} / {} = {}'.format(num1, num2, num1 / num2))

    elif operator == '**':

        print('{} ** {} = {}'.format(num1, num2, num1 ** num2))

    elif operator == '%':

        print('{} % {} = {}'.format(num1, num2, num1 % num2))

    else:

        print('Invalid operator')

    #다시할래?

    again()





def again(): # 다시할래?

    calc_again = input('''

Do you want to calculate again?

Please type Y for YES or N for No

''')

    if calc_again.upper() == 'Y':

        calculator()

    elif calc_again.upper() == 'N':

        print('See you later.')

    else:

        again()





if __name__ == '__main__': # 프로그램 실행할때 메인함수

    welcome()

    calculator()

코드 돌리기

편의상 3과 -2로 모든 연산자를 돌립니다

함수 시작과 덧셈


# welcome()

Welcome to Calculator



# calculator()

Please type in the math operation you would like to complete:

+ for addition  

- for subtraction

* for multiplication

/ for division

** for power

% for modulo

Please enter the operator: 

+

Please enter the first number: 3

Please enter the second number: -2

3.0 + -2.0 = 1.0



# again()

Do you want to calculate again?

Please type Y for YES or N for No

y

뺄셈


# calculator()

Please type in the math operation you would like to complete:

+ for addition  

- for subtraction

* for multiplication

/ for division

** for power

% for modulo

Please enter the operator: 

-

Please enter the first number: 3

Please enter the second number: -2

3.0 - -2.0 = 5.0



# again()

Do you want to calculate again?

Please type Y for YES or N for No

y

곱셈


# calculator()

Please type in the math operation you would like to complete:

+ for addition  

- for subtraction

* for multiplication

/ for division

** for power

% for modulo

Please enter the operator: 

*

Please enter the first number: 3

Please enter the second number: -2

3.0 * -2.0 = -6.0



# again()

Do you want to calculate again?

Please type Y for YES or N for No

y

나눗셈


# calculator()

Please type in the math operation you would like to complete:

+ for addition  

- for subtraction

* for multiplication

/ for division

** for power

% for modulo

Please enter the operator: 

/

Please enter the first number: 3

Please enter the second number: -2

3.0 / -2.0 = -1.5



# again()

Do you want to calculate again?

Please type Y for YES or N for No

y

지수


# calculator()

Please type in the math operation you would like to complete:

+ for addition  

- for subtraction

* for multiplication

/ for division

** for power

% for modulo

Please enter the operator: 

**

Please enter the first number: 3

Please enter the second number: -2

3.0 ** -2.0 = 0.1111111111111111



# again()

Do you want to calculate again?

Please type Y for YES or N for No

y

나머지 + 함수 종료


# calculator()

Please type in the math operation you would like to complete:

+ for addition  

- for subtraction

* for multiplication

/ for division

** for power

% for modulo

Please enter the operator: 

%

Please enter the first number: 3

Please enter the second number: -2

3.0 % -2.0 = -1.0



# again()

Do you want to calculate again?

Please type Y for YES or N for No

n

See you later.

'파이썬으로 수학 풀기' 카테고리의 다른 글

파이썬으로 그래프 그리기 1-1  (0) 2017.12.14
파이썬으로 그래프 그리기 - 1  (0) 2017.12.11
홀수 짝수 자판기  (0) 2017.12.10
구구단  (0) 2017.12.10
소인수분해  (0) 2017.12.10
Comments