トップ «前の日記(2010-06-13(Sun)) 最新 次の日記(2010-06-20(Sun))» 編集

屑俺日記

僕の備忘録(PC、UN*X、ネットワーク関連が中心)なんです。
自分の書いたところは適当(な時とか)に書き換えますので御了承を。


2010-06-16(Wed) 雨は止んだけど、湿っぽいまんま

例外もう一つ

閏年かどうかの判定に、例外処理を入れてみた。

#!/usr/bin/env python3
import sys
 
def leap(x):
  if(x % 4 == 0 and x % 100 != 0) or ( x % 400 == 0):
    print("{} is a leap year".format(x))
  else: print("{} is not a leap year".format(x))
 
try:
  x = int(sys.argv[1])
  leap(x)
except:
  print("There is no parameter or wrong ones.")
$ ./leap.py ahoka
There is no parameter or wrong ones.
$ ./leap.py
There is no parameter or wrong ones.
 
$ ./leap.py 1900
1900 is not a leap year
$ ./leap.py 2000
2000 is a leap year
$ ./leap.py 2010
2010 is not a leap year

リンクはご自由にどうぞ。でもURLや内容が変った場合はあしからず。

index.htmlは ここから。