Leap Year - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Leap Year (/thread-2352.html) Pages:
1
2
|
Leap Year - MartinEvtimov - Mar-09-2017 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 RE: Leap Year - buran - Mar-09-2017 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. RE: Leap Year - wavic - Mar-09-2017 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 topAlso, if one enter something that cannot be converted to in like text for example the script will stop with ValueError RE: Leap Year - buran - Mar-09-2017 (Mar-09-2017, 07:07 AM)wavic Wrote: Or put if year % 100 ==0: on topif 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.
RE: Leap Year - wavic - Mar-09-2017 Yes, I miss that. Put it on the top only is not going to work RE: Leap Year - Ofnuts - Mar-09-2017 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)
RE: Leap Year - Larz60+ - Mar-09-2017 The truth table is:
RE: Leap Year - Ofnuts - Mar-09-2017 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.. RE: Leap Year - buran - Mar-09-2017 :-) 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()
RE: Leap Year - wavic - Mar-09-2017 So you choose the easy way? |