+ 1
Need help with daysinmonth() function: my 'else' for def daysinmonth(x, y) is not working it should give out '31' as answer
def isleap(y): if y % 400 == 0: return True elif y % 100 == 0: return False elif y % 4 == 0: return True else: return False # x is month and y is year def daysinmonth(x, y): if x == 2: isleap (y) if isleap(y) == True: return 29 else: return 28 elif (x == 9, 4, 6, 11): return 30 else: return 31 print (daysinmonth(3,2020 )) # my 'else' for def daysinmonth(x, y) is not working it should give out '31' as answer instead it gives '30'; so maybe theres problem with elif or else
1 Réponse
+ 5
The way you have done the else condition does not work as expexted. when having x = 3, the expression x == 9,4,6,11 gives:
(False, 4, 6, 11) This is a tuple.
better use in operator:
elif x in [9, 4, 6, 11]: # you can omit the parenthesis !
this will return 31.