소스 파일명: testIf.py

  1. # coding=euc-kr
  2. """
  3.    Filename: testIf.py
  4.    Purpose:  Example using the conditional control structure syntax
  5.                  if .... else ...
  6.    Execute: python testIf.py [number]
  7. """
  8. import sys    # sys.argv를 위한 수입(import) 구문
  9. # 사용법을 보여주는 함수
  10. def printUsing():
  11.    print("Using: python testIf.py [number]")
  12.    print("This determines whether the number is positive or not.")
  13. # 명령행 인자의 개수가 1이 아니면 사용법을 보여준다.
  14. if len(sys.argv) != 2:
  15.     printUsing()
  16.     sys.exit(1)
  17. # 명령행 인자의 스트링을 가져와서
  18. # 배정밀도 부동소수점수로 변환하여
  19. # 변수 val에 저장한다.
  20. val = float(sys.argv[1])
  21. # 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  22. # 판단하는 if...else... 조건문
  23. if val > 0.0:
  24.     print("%g is a positive number." % val)
  25. elif val < 0.0:
  26.     print("%g is a negative number." % val)
  27. else:
  28.     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에서도 수정 없이 그대로 실행된다.




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,