본문 바로가기

컴퓨터/Python

단축평가

# -*- coding: euc-kr -*-

################################

a = 0

if a & 10/a:

print("a가 0입니다.")

else:

print("에러가 없이 통과!")


Traceback (most recent call last):

  File "/Users/MGP/Documents/workspace/Python/src/Python3/2/python.py", line 4, in <module>

    if a & 10/a:

ZeroDivisionError: division by zero



& 대신 and를 사용해 단축 평가가 이뤄져 예외가 발생하지 않음

# -*- coding: euc-kr -*-

################################

a = 0

if a and 10/a:

print("a가 0입니다.")

else:

print("에러가 없이 통과!")

에러가 없이 통과!


'컴퓨터 > Python' 카테고리의 다른 글

break, continue, 그리고 else  (0) 2013.07.12
while 문, for 문  (0) 2013.07.09
조건식의 참/거짓 판단  (0) 2013.07.09
if 문  (0) 2013.07.09
제너레이터  (0) 2013.07.09