Python Error “list Indices Must Be Integers Not Nonetype”
I am new to python and learning the language from Udacity. I wanted to write a python program that takes 2 dates and outputs the day difference between these 2 dates, assuming the
Solution 1:
You are missing a return
in your is_leap
function:
def isLeap(year):
if year%400==0:
returnTrueelse:
if year%100==0:
returnFalseelse:
if year%4==0:
returnTrueelse:
returnFalse # <-- here!
Otherwise, this function will implicitly return None
in that place, which is non-truthy, but not a bool
, and therefore not an int
(bool
is a subclass of int
, which makes the 0-1-index magic possible in the first place) that can be used as a list
index. Btw, you do not need the else
if there is a return
in the if
block:
def isLeap(year):
if notyear%400:
returnTrue
if notyear%100:
returnFalse
# returnnotyear%4 # is also possible here
if notyear%4:
returnTruereturnFalse # <-- needed to avoid None being returned
Whether this is more readable very often depends on the concrete circumstances. But here, with multiple nested branches, I think it helps keeping the indentation levels low and understanding what is happening.
Post a Comment for "Python Error “list Indices Must Be Integers Not Nonetype”"