2016-10-09 28 views
-3

我正在做一個程序來計算,如果給定的輸入是一個閏年或沒有。我在使用Anaconda Shell的Windows 10上使用Python 2.7。 這是一個愚蠢的程序,我不明白爲什麼我得到這個錯誤。這個特定的程序有幾個解決方案在線,但我想讓我的版本工作。請幫忙!「詮釋」對象不能迭代閏年程序

我收到錯誤消息「TypeError:'int'object is not iterable」 我試着用Google搜索一些東西並查看一些以前的答案,但是我很困惑!

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

您正在試圖解開的'範圍(0,9)'作爲2元組的每個值。你期望'ij00'做什麼? – vaultah

+1

公平起見,'int'對象不可迭代*任何*程序,閏年或其他。 – jonrsharpe

+1

你*寫*:如果年%4!= 0:返回false:elif year%100!= 0:返回true:else return year%400 == 0'而不是有兩個函數。 - 雖然這不是你的問題。 –

回答

2

我想你嘗試寫

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

但我認爲你嘗試檢查在user_input的最後兩個數字,所以你不需要for

如果number具有00末然後number % 100 == 0(這裏%裝置功能modulo

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

謝謝furas,幫助。我犯了一個非常愚蠢的錯誤。 –

相關問題