Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
new to teaching python to yr10's. This appears to work when telling the end user if their year is a leap year or not. But can someone please confirm if this code is working or the best way to do this. I realise there are probably different methods...Johnny:)
leapYear = int(input("what year is it?:"))
if (leapYear %4) == 0:
print ("Thats a leap year")
elif (leapYear %100)==0:
print ("thats not a leap year")
elif (leapYear % 400)== 0:
print ("Thats a leap year")
else:
print("thats not a leap year")
Here are three alternative ways to check for a leap year in python, with the 2nd method being an improved version of your own attempt:
1) Using calendar:
import calendar
calendar.isleap(year)
2) Similar to your own attempt but taking out the redundant steps and converting it into a method:
def is_leap_year(year):
if year % 100 == 0:
return year % 400 == 0
return year % 4 == 0
3) Check if the year provided has a a 29th of February using datetime:
import datetime
def is_leap_year(year):
try:
datetime.date(year, 2, 29)
except ValueError:
return False
return True
N.B. Try to teach your students to use snake_case in python code rather than camelCase.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
It's works on "Not leap year" but it doesn't work on "Leap year":
month=int(input("Enter a month: "))
year=int(input("Enter a year: "))
if year%4==0 and year>=0:
if year%100!=0:
year="Leap Year"
elif year%100==0:
if year%400==0:
year="Leap year"
else:
year="Not leap year"
elif year<0:
print("Please enter a valid number")
elif year%4!=0 and year>0:
year="Not leap year"
elif month<1 and month>12:
print("Please enter a valid number")
if year=="Leap year":
if month==2:
print("Number of days:29")
elif month==1 or 3 or 5 or 7 or 8 or 10 or 12:
print("Number of days:31")
else:
print("Number of days:30")
if year=="Not leap year":
if month==2 :
print("Number of days:28")
elif month==1 or 3 or 5 or 7 or 8 or 10 or 12:
print("Number of days:31")
else:
print("Number of days:30")
In leap years ı think it's going in if state and cant go from if statement. How can ı fix that. However in not leap years it works because its get in elif statment.
The problem is with this line:
year="Leap Year"
"Leap Year" is not the same as "Leap year" because string comparison is case sensitive in python as well as in most programming languages.
In general it's not great practice to hardcode strings in multiple places. As other people have mentioned you don't probably don't want to reuse the year var in case you need it again. You should try using a new var and saving a boolean (true or false) value. Maybe name it isLeapYear or something like that.
When you are declaring year="Leap Year", there are different spellings at different lines. At some point, it is "Leap Year" and at some point it is "Leap year". You need to be case sensitive in python. Try writing only "Leap year" everywhere and comparing year=="Leap year" in if statement
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The question is this:
We add a Leap Day on February 29, almost every four years. The leap day is an extra, or intercalary day and we add it to the shortest month of the year, February.
In the Gregorian calendar three criteria must be taken into account to identify leap years:
The year can be evenly divided by 4, is a leap year, unless:
The year can be evenly divided by 100, it is NOT a leap year, unless:
The year is also evenly divisible by 400. Then it is a leap year.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.
This is what I've coded in python 3
def is_leap(year):
leap = False
# Write your logic here
if((year%4==0) |(year%100==0 & year%400==0)):
leap= True
else:
leap= False
return leap
year = int(input())
print(is_leap(year))
This code fails for input 2100. I'm not able to point out the mistake. Help please.
First of all, you are using bitwise operators | and & (you can read about it here - https://www.educative.io/edpresso/what-are-bitwise-operators-in-python), but you need to use logical operators, such as or and and.
Also, your code can be simplified:
def is_leap(year):
return (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))
try this:
def leap_year(n):
if (n%100==0 and n%400==0):
return True
elif (n%4==0 and n%100!=0):
return True
else:
return False
This question already has answers here:
String formatting: % vs. .format vs. f-string literal
(16 answers)
Closed 3 years ago.
I'm new to Python and we're currently learning how to use if/elif/else and as a exercise our prof. wants us to write a program that answers if a year is a leap year or not.I found a guide that shows and gives you a pretty good explanation on how to write such a program.
The code looks like this:
year = int(input("Please Enter the Year Number you wish: "))
if (year%400 == 0):
print("%d is a Leap Year" %year)
elif (year%100 == 0):
print("%d is Not the Leap Year" %year)
elif (year%4 == 0):
print("%d is a Leap Year" %year)
else:
print("%d is Not the Leap Year" %year
The only thing I am trying to figure out but haven't been able to find a good answer to is why the author uses print("%d this is a leap year" %year)
How come %d when the running the program doesn't show up as %d when inside a string?
Here %d inside the string is a format specifier which means that it will be replaced with the integer value provided after the end of the string.
It is just a placeholder for the year value.
%d means you are expecting an integer int32 after the string which in your case is the year after each print statement.
for the same example you cna use Format method as well
print ("is a Leap Year : {}".format(year))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I made a birthday program in Python, the program ask from the user the month he were born in, and then calculate the months until the user have birthday.
For example, the user enter the number 5 and the current month is 2, the program output "You have 3 months until your birthday!".
I don't know how to calculate the months until he have birthday correct.
Example:
from datetime import datetime
def Birthday():
CurrentMonth = datetime.now().month
BornIn = input("What month were you born in ? - ")
result = int(BornIn) + CurrentMonth
if int(BornIn) == CurrentMonth:
print("You have already Birthday in this month!")
elif int(BornIn) > 12:
print("Invalid Input!")
else:
print("You have", result , "monthes until your Birthday!")
Birthday()
What mathematical action do I need to do, to calculate the months until his birthday?
Look at line 7, I used + to calculate but obviously it won't work.
Edit:
I need to do result = CurrentMonth - int(BornIn). This should fix the problem.
Your operation is not correct :
You should do:
result = (int(BornIn) - CurrentMonth)%12
Modulo is here to manage case when your birthday is in the next year. Without modulo you will have negative results (here you won't have any problem because we are in December so your birthday can't be in the 13th months)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have the following program to find out the leap year
year = int(input("Enter a year: ")
if (year % 4 == 0) and (year % 100 == 0) and (year % 400 == 0):
print("True")
else:
print("False")
However, I'm getting following syntax error
File "is_leap.py", line 2
if (year % 4) and (year % 100) and (year % 400):
^
I do not understand why it is showing syntax error in :
P.S: I am newbie to Python and programming, and I knew there are solutions in online to figure out leap year. But I'm trying to figure this in my way.
I'm trying to understand why it's showing this syntax error while the "if" condition should have : in Python.
That line itself is fine.
You're missing a closing parenthesis on the line before it:
year = int(input("Enter a year: ")
^^^^
P. S. Not directly related to your question, but to make the leap calculation work you'll need to invert some of the conditions in that if statement -- I'll let you figure out the details yourself.