2016-10-09 5 views
-3

与えられた入力がうるう年かどうかを計算するプログラムを作成しています。 Anaconda ShellでWindows 10でPython 2.7を使用しています。 これはばかげたプログラムで、なぜこのエラーが出るのかわかりません。この特定のプログラムにはいくつかの解決策がオンラインでありますが、私は自分のバージョンを動作させたいと思っています。助けてください!"Int"オブジェクトはうるう年プログラムのために反復できません

「TypeError: 'int'オブジェクトが反復可能ではありません」 私はグーグルを試してみましたが、これまでのいくつかの回答を見ていましたが、混乱しています!

def sort_year(user_input): 
    for i,j in range(0,9): 
     if user_input == [ij00]: 
      leap_yr1(user_input) 
     else: 
      leap_yr2(user_input) 

def leap_yr1(user_input): 
    if user_input % 400 == 0: 
     print("The given year is a leap year") 
    else: 
     print("The given year is not a leap year") 

def leap_yr2(user_input): 
    if user_input % 4 == 0: 
     print("The given year is a leap year") 
    else: 
     print("The given year is not a leap year") 

print("Which year do you want to check?: ") 
user_input = int(raw_input()) 
sort_year(user_input) 
+3

を有している場合は、 '2タプル(9、0)'範囲の各値をアンパックしようとしています。 'ij00'は何を期待していますか? – vaultah

+1

公正であるためには、 'int'オブジェクトは*プログラム、うるう年、またはそれ以外の場合には反復できません。 – jonrsharpe

+1

2つの関数を持つのではなく、 'year%4!= 0の場合、falseを返します:elif year%100!= 0:trueを返します。 - それはあなたの問題ではありません。 –

答えて

2

私はあなたが

for i in range(0,9): 
    for j in range(0,9): 

を書き込もうと思います。しかし、私はあなたがそうあなたがforを必要としないuser_inputで最後の二つの数字をチェックしてみてくださいね。

number次いで終わりに00number % 100 == 0(ここで%機能moduloを意味する)

def sort_year(user_input): 
    if user_input % 100 == 0: 
     leap_yr1(user_input) 
    else: 
     leap_yr2(user_input) 
+0

おかげで、助けてくれました。私は非常にばかげた間違いをしていた。 –

関連する問題