Bottom Page

• 1 Vote(s) - 1 Average
• 1
• 2
• 3
• 4
• 5
 Leap Year MartinEvtimov Programmer named Tim Posts: 5 Threads: 3 Joined: Feb 2017 Reputation: 0 Likes received: 0 #1 Mar-09-2017, 06:19 AM (This post was last modified: Mar-09-2017, 06:37 AM by Larz60+. Edited 1 time in total. Edit Reason: added cde tags ) ```def get_year():    print("Please input year:")    year = int(input())    return year def if_else(year):    if (year % 4 == 0):      print("It's a leap year")    elif (year % 400 == 0):      print("It's a leap year")    elif (year % 100 == 0):      print("It's not a leap year")    else:      print("There is an error") def main():    years = get_year()    if_else(years)```    when I input 2400(not a leap year) it says that its a leap year :( Please help buran Posts: 5,477 Threads: 98 Joined: Sep 2016 Reputation: 405 Likes received: 1171 #2 Mar-09-2017, 07:02 AM (This post was last modified: Mar-09-2017, 07:03 AM by buran. Edited 2 times in total.) Please, wrap your code in code tags. OK, this Larz60+ added code tags for you. Anyway, in the future always wrap your code in code tags. 2400 IS a leap year - it's exactly divisible by 400. Quote:Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 were not leap years, but the years 1600 and 2000 were. wavic So-and-so of the Yard Posts: 2,895 Threads: 39 Joined: Sep 2016 Reputation: 89 Likes received: 364 #3 Mar-09-2017, 07:07 AM (This post was last modified: Mar-09-2017, 07:07 AM by wavic. Edited 1 time in total.) Hello! Put print() in each loop to see where it returns True. For the year 2400, this will happen in the first loop because 2400 % 4 == 0 is True and will print that this year leap. Change the conditions ```def get_year():    print("Please input year:")    year = int(input())    return year   def if_else(year):    if year % 4 == 0 and year % 100 != 0:      print("It's a leap year")    elif year % 400 == 0 and year % 100 != 0:      print("It's a leap year")    elif (year % 100 == 0):      print("It's not a leap year")    else:      print("There is an error")   def main():    years = get_year()    if_else(years) ```Or put `if year % 100 ==0:` on top Also, if one enter something that cannot be converted to in like text for example the script will stop with ValueError "As they say in Mexico 'dosvidaniya'. That makes two vidaniyas." https://freedns.afraid.org buran Posts: 5,477 Threads: 98 Joined: Sep 2016 Reputation: 405 Likes received: 1171 #4 Mar-09-2017, 07:51 AM (This post was last modified: Mar-09-2017, 07:51 AM by buran. Edited 4 times in total.) (Mar-09-2017, 07:07 AM)wavic Wrote: Or put if year % 100 ==0: on top if he puts this on the top then it will not catch years divisible by 400 - this will be false negative! The code works as expected, but as you mention he needs to catch incorrect user input, but also can have function that checks leap year or not and return True/False respectively. This will allow to reuse this function in the future, instead of `calendar.isleap()`, which is available. wavic likes this post wavic So-and-so of the Yard Posts: 2,895 Threads: 39 Joined: Sep 2016 Reputation: 89 Likes received: 364 #5 Mar-09-2017, 08:33 AM Yes, I miss that. Put it on the top only is not going to work  "As they say in Mexico 'dosvidaniya'. That makes two vidaniyas." https://freedns.afraid.org Ofnuts Norwegian Blue Posts: 687 Threads: 37 Joined: Sep 2016 Reputation: 24 Likes received: 218 #6 Mar-09-2017, 08:56 AM Actually you have to do the test the other way: test for 400 then 100 then 4. @MartinEvtimov: use raw_input() instead of input() if you use Python v2, or make sure you use Python v3 (your `print(...)` with parentheses are normally the v3 syntax) Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V. Your one-stop place for all your GIMP needs: gimp-forum.net Larz60+ aetate et sapientia Posts: 8,589 Threads: 310 Joined: Sep 2016 Reputation: 357 Likes received: 1163 #7 Mar-09-2017, 02:45 PM The truth table is: ``````Output: Truth Table ---------------------------------------------------- | Div by 4? | Div by 100? | Div by 400? | Is Leap? | ---------------------------------------------------- |     T     |      T      |      T      |    T     | ---------------------------------------------------- |     T     |      T      |      F      |    F     | ---------------------------------------------------- |     T     |      F      |      T      |    T     | ---------------------------------------------------- |     T     |      F      |      F      |    T     | ---------------------------------------------------- |     F     |      T      |      T      |    F     | ---------------------------------------------------- |     F     |      T      |      F      |    F     | ---------------------------------------------------- `````` wavic likes this post Ofnuts Norwegian Blue Posts: 687 Threads: 37 Joined: Sep 2016 Reputation: 24 Likes received: 218 #8 Mar-09-2017, 04:42 PM A "truth" table that shows a number divisible by 400 and not by 4 or not by 100, hmmph. Welcome to "alternate mathematics" :) Otherwise since programming languages do not support "unless"(*), `If ConditionA unless ConditionB` and be rewritten `if ConditionB else if Condition A`. (*) of course some smart-aleck will come up with one.. Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V. Your one-stop place for all your GIMP needs: gimp-forum.net buran Posts: 5,477 Threads: 98 Joined: Sep 2016 Reputation: 405 Likes received: 1171 #9 Mar-09-2017, 05:08 PM :-) ```from calendar import isleap def is_leap(year):    return not (year%400 and not year%100 or year%4) for year in (1900, 2017, 2000, 2016):    print year, is_leap(year), isleap(year) # year, is_leap(), calendar.isleap() `````````Output:1900 False False 2017 False False 2000 True True 2016 True True [Finished in 0.04s]`````` wavic So-and-so of the Yard Posts: 2,895 Threads: 39 Joined: Sep 2016 Reputation: 89 Likes received: 364 #10 Mar-09-2017, 05:30 PM So you choose the easy way?  "As they say in Mexico 'dosvidaniya'. That makes two vidaniyas." https://freedns.afraid.org « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Creating folders dynamically with year/month/date sritsv 0 2,836 Oct-16-2017, 03:44 PM Last Post: sritsv Calculating what year it is when you are 100 years old dankim9000 2 5,319 Apr-26-2017, 08:03 PM Last Post: dankim9000 leap year program issue jashajmera 3 1,603 Feb-04-2017, 11:51 AM Last Post: ichabod801 Logic to convert an integer to YEAR / MONTH hugobaur 9 2,979 Oct-18-2016, 11:58 AM Last Post: hugobaur

Forum Jump:

Users browsing this thread: 1 Guest(s)