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
Related
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 1 year ago.
Improve this question
I am in Ap computer science( i will never touch CS after this ugghhh) and have to do this python problem using While loops. The problem is:
"As a future athlete you just started your practice for an upcoming event. On the first day you run x miles, and by the day of the event you must be able to run y miles.
Calculate the number of days required for you to finally reach the required distance for the event, if you increases your distance each day by 10% from the previous day.
Print one integer representing the number of days to reach the required distance"
and so far i've come up with this:
x = int(input())
y = int(input())
days=1
while x<y:
x=x*0.1
days= days+1
you should increment your day then print it out your while loop, also only multiplying by 0.1 is like dividing by 10 so you should not forget to add your x value, try like this:
x = int(input("On the first day you run: \n"))
y = int(input("and by the day of the event you must be able to run:\n"))
day = 1
while x<y:
x += x+x*0.1
day += 1
print(f'you should run {day} days')
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
I need to create a code that returns the following situation. A date range with only month and year using while and exporting it to a text file using Pandas
I created the first while but I need to add a second one that adds a year each time the month reaches 12:
mnst=6
yrst=2010
mned=12
yred=2020
while mnst<=mned:
print(mnst,"/",yrst)
mnst + 1
You could use a generator function:
def iterate_ym(yr, mn, yred, mned):
while (yr, mn) <= (yred, mned):
yield (yr, mn)
mn += 1
if mn > 12:
yr += 1
mn = 1
for yr, mn in iterate_ym(2010, 6, 2020, 12):
print(yr, mn)
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.
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.