프로그래밍/Python
if...else... 조건문 사용 예제 for Python
Scripter
2008. 2. 19. 15:40
소스 파일명: testIf.py
- # coding=euc-kr
- """
- Filename: testIf.py
- Purpose: Example using the conditional control structure syntax
- if .... else ...
- Execute: python testIf.py [number]
- """
- import sys # sys.argv를 위한 수입(import) 구문
- # 사용법을 보여주는 함수
- def printUsing():
- print("Using: python testIf.py [number]")
- print("This determines whether the number is positive or not.")
- # 명령행 인자의 개수가 1이 아니면 사용법을 보여준다.
- if len(sys.argv) != 2:
- printUsing()
- sys.exit(1)
- # 명령행 인자의 스트링을 가져와서
- # 배정밀도 부동소수점수로 변환하여
- # 변수 val에 저장한다.
- val = float(sys.argv[1])
- # 변수 val에 저장된 값이 양수인지 음수인지 0인지를
- # 판단하는 if...else... 조건문
- if val > 0.0:
- print("%g is a positive number." % val)
- elif val < 0.0:
- print("%g is a negative number." % val)
- else:
- print("%g is zero." % val)
실행> python testIf.py
Using: python testIf.py [number]
This determines whether the number is positive or not.
실행> python testIf.py 6.25
6.25 is a positive number.
실행> python testIf.py -6.25
-6.25 is a negative number.
실행> python testIf.py 0
0 is zero.
※ 위의 소스 코드는 Jython으로도 수정 없이 그대로 실행된다.
실행> jython testIf.py
Using: python testIf.py [number]
This determines whether the number is positive or not.
실행> jython testIf.py 1.02
1.02 is a positive number.
실행> jython testIf.py -1.02
-1.02 is a negative number.
실행> jython testIf.py 0
0 is zero.
위의 소스 코드는 ipy 명령에 의하여 IronPython에서도 수정 없이 그대로 실행된다.
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.