Python 으로 프로그래밍을 하다보면 의도했던, 의도하지 않았던 간에 예외를 처리해야 하는 상황에 맞닥드리게 됩니다. 이때 에러를 발생(Raise Error) 시킬 수도 있고 회피할 수도 있으며, 필요에 따라서는 예외 처리를 해야 할 수도 있습니다. 


이번 포스팅에서는 Python에서 예외를 처리하는 4가지 try, except, else, finally 절(clause)을 소개하겠습니다. 




  • try 에는 정상적으로 실행할 프로그램 코드(code block) 써줍니다. 

  • except 절에는 의 try 절에서 실행한 코드에 예외가 발생했을 경우에 실행할 코드를 써줍니다. 

  • else 절에는 앞의 try 절에서 실행한 코드에 예외가 발생하지 않은 경우에 실행할 코드를 써줍니다. 

  • finally 절에는 try 절에서의 예외 발생 여부에 상관없이 항상(always execute) 마지막에 실행할 코드를 써줍니다. 


간단한 예로서, 두개의 숫자를 매개변수로 받아서 나눗셈을 하는 사용자 정의함수를 try, except, else, finally 의 예외절을 사용하여 정의해 보겠습니다. 


try, except, else, finally 절의 끝에는 콜론(:)을 붙여주며, 그 다음줄에 코드 블락은 들여쓰기(indentation)을 해줍니다. 

except의 경우 'except:' 만 사용해도 되고, 아래 예의 'except ZeorDivisionError as zero_div_err:' 처럼 Built-in Exception Class를 사용해서 에러를 명시적으로 써줄 수도 있습니다. (본문의 제일 밑에 Python Built-in Exception Class 의 계층 구조 참조)



# Python Exceptions: try, except, else, finally

def divide(x, y):

    try:

        result = x / y

    except ZeroDivisionError as zero_div_err:

        print("Except clause:", zero_div_err)

    else:

        print("Else clause: The result of division is", result)

    finally:

        print("Finally clause is executed.")

 




(1) try 절 정상 실행 시 (executed nomally): try --> else --> finally


두 숫자 x, y를 인자로 받아서 나눗셈을 하는 사용자 정의함수 divide(x, y) 에 x=1, y=2 를 넣어서 정상적으로 코드가 수행되었을 때의 결과를 보겠습니다.  마지막으로 finally 절이 실행되었습니다. 


In [1]: def divide(x, y):

   ...: try:

   ...: result = x / y

   ...: except ZeroDivisionError as zero_div_err:

   ...: print("Except clause:", zero_div_err)

   ...: else:

   ...: print("Else clause: The result of division is", result)

   ...: finally:

   ...: print("Finally clause is executed.")

   ...:

   ...:

In [2]: divide(1, 2)

Else clause: The result of division is 0.5

Finally clause is executed. 




(2) try 절 실행 중 예외 발생 시 (exception occurred): try --> except --> finally


1을 '0'으로 나누라고 하면 'except ZeroDivisionError as zero_div_err:' 의 except 절이 실행됩니다. 그리고 마지막으로 finally 절이 실행되었습니다. 


In [3]: divide(1, 0)

Except clause: division by zero

Finally clause is executed.




(3) try 절 Error 발생 시: try --> finally  --> Raise Error


divide(x, y) 사용자 정의함수에 숫자를 인자로 받는 매개변수에 문자열(string)을 입력하는 경우 TypeError가 발생하겠지요? 이때 finally를 먼저 실행하고, 그 후에 TypeError 를 발생시킵니다. 


In [4]: divide("1", "2")

Finally clause is executed.

Traceback (most recent call last):


File "<ipython-input-4-bbf78a5b43b9>", line 1, in <module>

divide("1", "2")


File "<ipython-input-1-78cbb56f9746>", line 3, in divide

result = x / y


TypeError: unsupported operand type(s) for /: 'str' and 'str'


Traceback (most recent call last):


File "<ipython-input-4-bbf78a5b43b9>", line 1, in <module>

divide("1", "2")


File "<ipython-input-1-78cbb56f9746>", line 3, in divide

result = x / y


TypeError: unsupported operand type(s) for /: 'str' and 'str' 




(4) Try Except 절에서 에러 발생 시 Exception Error 이름과 Error message 출력



import numpy as np


arr = np.array([[1., 2., 3.], [4.0, 5., 6.]])

print(arr)

[Out]
[[1. 2. 3.]
 [4. 5. 6.]]


# It will not work and eraise an error

try:

    arr.reshape(5, -1)

except Exception as e:

    print(f"{type(e).__name__}: {e}")

[Out] 
ValueError: cannot reshape array of size 6 into shape (5,newaxis)





[ 참고: The Class Hierarchy of Python Built-in Exceptions ]

(* source: https://docs.python.org/3/library/exceptions.html)


 BaseException

 +-- SystemExit

 +-- KeyboardInterrupt

 +-- GeneratorExit

 +-- Exception

      +-- StopIteration

      +-- StopAsyncIteration

      +-- ArithmeticError

      |    +-- FloatingPointError

      |    +-- OverflowError

      |    +-- ZeroDivisionError

      +-- AssertionError

      +-- AttributeError

      +-- BufferError

      +-- EOFError

      +-- ImportError

      |    +-- ModuleNotFoundError

      +-- LookupError

      |    +-- IndexError

      |    +-- KeyError

      +-- MemoryError

      +-- NameError

      |    +-- UnboundLocalError

      +-- OSError

      |    +-- BlockingIOError

      |    +-- ChildProcessError

      |    +-- ConnectionError

      |    |    +-- BrokenPipeError

      |    |    +-- ConnectionAbortedError

      |    |    +-- ConnectionRefusedError

      |    |    +-- ConnectionResetError

      |    +-- FileExistsError

      |    +-- FileNotFoundError

      |    +-- InterruptedError

      |    +-- IsADirectoryError

      |    +-- NotADirectoryError

      |    +-- PermissionError

      |    +-- ProcessLookupError

      |    +-- TimeoutError

      +-- ReferenceError

      +-- RuntimeError

      |    +-- NotImplementedError

      |    +-- RecursionError

      +-- SyntaxError

      |    +-- IndentationError

      |         +-- TabError

      +-- SystemError

      +-- TypeError

      +-- ValueError

      |    +-- UnicodeError

      |         +-- UnicodeDecodeError

      |         +-- UnicodeEncodeError

      |         +-- UnicodeTranslateError

      +-- Warning

           +-- DeprecationWarning

           +-- PendingDeprecationWarning

           +-- RuntimeWarning

           +-- SyntaxWarning

           +-- UserWarning

           +-- FutureWarning

           +-- ImportWarning

           +-- UnicodeWarning

           +-- BytesWarning

           +-- ResourceWarning



많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)



728x90
반응형
Posted by Rfriend
,

외국의 블로그에 소프트엔지니어 채용 과정 중에 코딩 인터뷰에서 있을 법한 문제로 회자되는 "FizzBuzz" 문제가 있습니다. 


"FizzBuzz"문제는 


 - 1에서 100까지의 숫자를 출력하되, 

 - 만약 해당 숫자가 '3'으로 나누어지면 숫자 대신에 "Fizz"를 출력하고, 

 - 만약 해당 숫자가 '5'로 나누어지면 숫자 대신에 "Buzz"를 출력하며, 

 - 만약 해당 숫자가 '3'과 '5'로 모두 나누어지면 숫자 대신에 "FizzBuzz"를 출력


하는 코드는 짜라는 것입니다.  


1~100까지 하기에는 좀 길기 때문에 1~30까지의 숫자에 대해서만 FizzBuzz 문제를 풀어보면 아래의 Original -> FizzBuzz 변환 전/후 비교를 보시면 이해가 쉬울 것입니다. 




위의 FizzBuzz 문제를 푸는 방법이 여러가지가 있을 수 있을 텐데요, (1) if, elif, else 조건문 사용, (2) if, elif, else 조건문을 포함한 사용자 정의 함수, (3) 문자열 합치기 (concatenating strings) 의 세 가지 방법을 소개합니다. 



  (1) if, elif, else 조건문을 사용한 FizzBuzz 코딩



# (1) if, elif, and else

for num in range(1, 31):

    if num % 3 == 0 and num % 5 == 0:

        print('FizzBuzz')

    elif num % 3 == 0:

        print('Fizz')

    elif num % 5 == 0:

        print('Buzz')

    else:

        print(num)

 




  (2) if, elif, else 조건문과 사용자 정의 함수(UDF)을 사용한 FizzBuzz 코딩


사용자 정의 함수에 대해서는 조만간 포스팅 하겠습니다. ^^



# (2) User Defined Function

def fizzbuzz(num):

    if num % 3 == 0 and num % 5 == 0:

        return 'FizzBuzz'

    elif num % 3 == 0:

        return 'Fizz'

    elif num % 5 == 0:

        return 'Buzz'

    else:

        return str(num)

    

print("\n".join(fizzbuzz(num) for num in range(1, 31)))

 




  (3) 문자열 합치기(Concatenating Strings)을 이용한 FizzBuzz 코딩



# (3) Concatenating Strings

for num in range(1, 31):

    string = ""

    if num % 3 == 0:

        string = string + "Fizz"

    if num % 5 == 0:

        string = string + "Buzz"

    if num % 5 != 0 and num % 3 != 0:

        string = string + str(num)

    print(string)

 



아래 링크는 취업 인터뷰에서 FizzBuzz 코딩 문제를 듣고서 Tensorflow로 딥러닝 모형을 적용했다가 취업 실패했다는 (가상의?) 블로그 내용입니다.  


Fizz Buzz in Tensorflow (http://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/)


간단한 '주먹지르기'로 해결할 수 있는 문제를 단지 '최신 유행'하는 기술이라는 이유만으로  '공중 3회전 날아 뒤돌아 발 차고 손목날로 목 가로지르기' 기술을 적용할 필요가 있는가에 대해서 우스개 (가상?) 사례로 소개한 포스팅인 듯 합니다.  Interviewer 의 반응을 유심히 보면서... 인터뷰 과정을 한번 보시는 것도 재미있을 것 같습니다. ㅋㅋ


많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾸욱 눌러주세요. 



728x90
반응형
Posted by Rfriend
,

지난 포스팅에서는 파이썬 프로그래밍에 들어가기에 앞서 파이썬이 어떻게 참(True), 거짓(False)을 평가, 판단하는지에 대한 기본 문법을 알아보았습니다. 


이번 포스팅부터는 파이썬 프로그램의 흐름을 제어(Python progmram flow control)하는 방법에 대해서 몇 개 포스팅으로 나누어서 소개를 하겠습니다. 


파이썬 프로그램 흐름 제어에서 알아야 할 큰 2가지 뼈대가 있다면 


- (1) 조건이 참(True), 거짓(False)인지에 따라 프로그램 흐름을 나누어 주는 분기문(Branch statements)

       : if statements

       : if, else statements

       : if, elif, else statements

       : nested if statements


- (2) 조건이 참이거나 순서열의 끝까지 반복, 중단하게 해주는 반복문

       : while

       : for

       : continue, break


가 있습니다. 


이번 포스팅에서는 먼저 if, elif, else 분기문(Branch statement)에 대해서 알아보겠습니다. 



[ 파이썬 프로그램 흐름 제어 (Python program flow control) ]






먼저 if, else 분기문에서 사용하는 용어를 간단히 짚어보고 넘어가겠습니다. 


  • if 조건문에는 참(True), 거짓(False)을 평가할 수 있는 조건을 써주고, 마지막에는 콜론(:)을 붙여줍니다.
  • if 조건문이 거짓이면 'else:' 조건문으로 프로그램 흐름이 넘어가게 되며, 역시 마지막 부분에 콜론(:)을 붙여줍니다. 
  • if, else 조건문 아래의 컴퓨터에게 시킬 일을 적어놓은 부분은 'Code Block'라고 합니다. 
  • Code Block 은 '들여쓰기(indentation)'를 해서 구분을 해줍니다. 보통 space bar로 4칸 사용합니다. 
    (R, Java, C 등은 중괄호 { } 를 사용)


[ 파이썬 if, else 분기문 예시 (example of Python if, else branch statement) ]




이제 파이썬의 if, else 분기문을 가지고 간단한 프로그램을 하나 짜보도록 하겠습니다. 


 (1) 조건이 1개 일 때 if, else 분기문 (if, else branch statement with 1 condition)


파이썬이 해야 하는 일의 흐름은, 

  • (1-1) 파이썬 시험 점수를 입력받아서

  • (1-2) 만약 파이썬 시험 점수가 80점 이상이면 (if 조건문 True)
    "당신의 파이썬 점수는 xx점입니다. 축하합니다. 파이썬 시험을 통과하였습니다. :-)"
    를 출력하고, 종료. 

  • (1-3) 만약 파이썬 시험 점수가 80점 이상이 아니면 (if 조건문 False)
    "당신의 파이썬 점수는 xx점입니다. 파이썬 시험을 낙제했습니다. 공부 더 하세요. T_T"
    를 출력하고 종료. 

        [ Flow Diagram ]



위의 프로그램 흐름을 if, else 분기문을 사용해서 파이썬 프로그래밍을 해보면 아래와 같습니다. 



# (1) if, else statement


print('Put your Python test score : ')


py_score = int(input())


if py_score >= 80:

    

    print('Your Python score is {0}'.format(py_score))

    print('Congratulations! You passed Python test. :-)')

    

else:

    

    print('Your Python score is {0}'.format(py_score))

    print('You failed Python test. Study more! T_T')

 



아래에 파이썬 시험 점수가 90점일 때와 70점일 때 원하는 메시지대로 출력을 제대로 해주는지 시험해 보겠습니다. 



Put your Python test score :

90


Your Python score is 90

Congratulations! You passed Python test. :-)

 



Put your Python test score :

70


Your Python score is 70

You failed Python test. Study more! T_T

 



if 와 else 분기절의 마지막에 콜론(colon, :)을 실수로 빼먹으면 'SyntaxError: invalid syntax' 에러가 발생합니다. 



# If you miss colon(:) at the end, then 'SyntaxError: invalid syntax'

In [3]: print('Put your Python test score : ')

    ...:

    ...: py_score = int(input())

    ...:

    ...: if py_score >= 80 # Oops! Syntax Error due to no colon(:)

    ...:

    ...: print('Your Python score is {0}'.format(py_score))

    ...: print('Congratulations! You passed Python test. :-)')

    ...:

    ...:

    ...: else:

    ...:

    ...: print('Your Python score is {0}'.format(py_score))

    ...: print('You failed Python test. Study more! T_T')

    ...:

File "<ipython-input-18-b2c00ee4ee98>", line 5

if py_score >= 80 # Oops! Syntax Error due to no colon(:)

^

SyntaxError: invalid syntax

 




다음으로, 조건이 2개 이상일 때 if, elif, else 분기문 사용하는 법을 알아보겠습니다. elif 는 else if 의 줄임말로 보면 되겠습니다. 


 (2) 조건이 2개 일 때 if, elif, else 분기문 

      (if, elif, else branch statement with multiple condition expressions)


파이썬 시험 점수는 0점 ~ 100점 사이의 정수만 가능하고, 0점~100점 사이의 정수를 벗어나면 (예: -50점, 150점) '점수를 잘못입력했습니다'라는 안내 메시지를 출력하는 것으로 프로그램 코드를 좀더 똑똑하게 짜보겠습니다. 

  • (2-1) 파이썬 시험 점수를 입력받아서

  • (2-2) 만약 파이썬 시험 점수가 '80점 이상 ~ 100점 이하' 이면 
    "당신의 파이썬 점수는 xx점입니다. 축하합니다. 파이썬 시험을 통과하였습니다. :-)"를 출력하고, 종료. 

  • (2-3) 만약 파이썬 시험 점수가 '80점 이상 ~ 100점 이하'가 아니면서, '0점 이상 ~ 80점 미만'이면 
    "당신의 파이썬 점수는 xx점입니다. 파이썬 시험을 낙제했습니다. 공부 더 하세요. T_T"를 출력하고 종료. 

  • (2-4) 만약 파이썬 시험 점수가 '80점 이상 ~ 100점 이하'도 아니고, '0점 이상 ~ 80점 미만'도 아니면 
    "파이썬 시험 점수를 잘못 입력하였습니다" 를 출력하고 종료. 


       [ Flow Diagram ]




위의 (2-2) 프로그램 흐름을 파이썬 코드로 짜보면 아래와 같습니다.  if 조건문 안에 if 조건문을 중첩해서 쓸 수도 있구요, and, or 논리 연산자를 같이 쓸 수도 있습니다. 


# (2) if, elif, else statement


print('Put your Python test score : ')


py_score = int(input())


if py_score >= 80 and py_score <= 100:

    

    print('Your Python score is {0}'.format(py_score))

    print('Congratulations! You passed Python test')

    

elif py_score >= 0 and py_score < 80:

    

    print('Your Python score is {0}'.format(py_score))

    print('You failed Python test. Study more!')

    

else:

    

    print('You put the wrong score.')

 



파이썬 점수로 150점 (<- 잘못 입력), 90점(<- 시험 통과), 60점(<- 시험 낙제), -50점(<- 잘못 입력) 을 각 각 입력해보겠습니다. 



Put your Python test score : 

150


You put the wrong score.

 



Put your Python test score : 

90


Your Python score is 90

Congratulations! You passed Python test

 



Put your Python test score : 

60


Your Python score is 60

You failed Python test. Study more!

 



Put your Python test score : 

-50


You put the wrong score.

 




 (3) if 조건문 안에 if 조건문을 중첩해서 프로그래밍 하기 (nested if statements)


위의 (2)번 프로그램 흐름을 nested if statements를 사용해서 아래에 구현해 보았습니다. 위의 (2)번 if, elif, else 보다 더 복잡하고 코드도 길군요. 특히, nested if statements 를 사용할 때는 들여쓰기(indentation) 할 때 유의해야 합니다.  Spyder나 Pycharm 같은 IDE 의 Editor 창을 사용하면 자동으로 들여쓰기를 해줘서 실수를 방지하는데 도움이 됩니다. 



# (3) nested if statements

print('Put your Python test score : ')


py_score = int(input())


if py_score <= 100:

    if py_score >= 80:       

        print('Your Python score is {0}'.format(py_score))

        print('Congratulations! You passed Python test. :-)')

    

    else:       

        if py_score < 80:

            if py_score > 0:

                print('Your Python score is {0}'.format(py_score))

                print('You failed Python test. Study more! T_T')

                

            else:                

                print('You put the wrong score.')

        

else:    

    print('You put the wrong score.')

 



프로그램 흐름 제어 코드를 잘 짠건지 한번 시험을 해볼까요?  150점 (<- 잘못 입력), 90점 (<- 시험 통과), 60점 (<- 시험 낙제), -50점 (<- 잘못 입력)을 각 각 입력해보겠습니다.  아래 결과를 보니 파이썬 프로그램 코드를 제대로 짰네요. ^^



Put your Python test score : 

150


You put the wrong score.

 



Put your Python test score : 

90


Your Python score is 90

Congratulations! You passed Python test. :-)

 



Put your Python test score : 

60


Your Python score is 60

You failed Python test. Study more! T_T

 



Put your Python test score : 

-50


You put the wrong score.

 



많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾸욱 눌러주세요. 


다음 포스팅에서는 반복문(Loop statement)에 대해서 알아보겠습니다. 



728x90
반응형
Posted by Rfriend
,