keira
keira

Reputation: 27

leap year python script

I am trying to write a code that will accept any of the months of the year and output the month entered plus a list of the days in the month. But if February is entered it should ask for the birth year and check if that is a leap year and output February plus the days in February. Where did I go wrong?

month = input("Enter any month of the year: ")
for n in month:
    if (n == "January" or 
     n == "March" or 
     n == "April" or
     n == "May" or 
     n == "June" or 
     n == "July" or 
     n == "August" or
     n == "September" or 
     n == "October" or 
     n == "November" or 
     n == "December"):
       print (n + range(1, 32))
 elif n == "February" :
    year = input("Enter a year")
    for i in year:
        if i % 4 == 0:
            print (year + range(1,29))

Upvotes: 1

Views: 334

Answers (3)

Trupal Patel
Trupal Patel

Reputation: 26

If you are asking where did you go wrong, there are some places:

  • Your if and elif are not indented properly, they do not align.
  • When you write for n in month:, n takes value of every letter in the month inputed. e.g. if you entered "July", n would be iterating as "J", "u", "l", and "y". So comparing n to full names of months will always evaluate to False. Just compare the month instead of iterating over it.
  • When taking input, python stores the value entered by user as string (year = input("Enter a year")). So when you are iterating over the variable year (for i in year) you are iterating over a string of numbers and not the number. Furthermore, modulus operator won't work for strings (i % 4) as i in this case is a string. Just do year = int(input("Enter a year")) to convert year to ineger and do not iterate over it, check for remainder with 4 directly.

You can refer to the code by paxdiablo to get a working code.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882028

Assuming your intent is to learn how to program (such as for a educational assignment), the following should help. If it's just to do the job, you should probably use some of the built-in Python modules (like datetime or calendar) since they'll do a lot of the hard work for you.


First, input returns a string and, when you iterate over a string (for n in month), you'll get one character at a time. So iterating over "May" will give you "M", "a", and "y", none of which are equal to "May". Don't iterate, just compare the entire string:

if month == "January" ...

Second, not all those months have 31 days, you'll need to distinguish those that have 30:

if month == "February":
    doFebStuff()
elif month in ['September', 'April', 'June', 'November']:
    do30DayStuff()
else:
    do31DayStuff()

Third, you can't concatenate a string and a range, you'll need to do something like:

print(month, end='')
for i in range(1, 32):
    print(f" {i}", end='')
print()

Fourth, again for the year, input gives you a string and you don't want to iterate over it. You also want to make it an integer if you're going to do mathematical calculations on it, something like:

year = int(input("Enter a year: "))

Fifth, the rules for leap years are slightly more complex than every four years, specifically:

  • if it's a multiple of 400, it's a leap year; else
  • if it's a multiple of 100, it's not; else
  • if it's a multiple of 4, it is; else
  • it's not.

Taking all that into account, here's one way to do it, with added checks for validity (month name and year):

Days30 = ["September", "April", "June", "November"]
Days31 = ["January", "March", "May", "July", "August", "October", "December"]

lastDay = None
month = input("Enter any month of the year: ")
if month == "February":
    lastDay = 28
    year = -1
    while year < 0:
        try:
            year = int(input("Enter the year: "))
        except:
            year = -1
    if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0):
        lastDay = 29
elif month in Days30:
    lastDay = 30
elif month in Days31:
    lastDay = 31

if lastDay is None:
    print(f"I don't know the month {month}")
else:
    print(f"{month}:", end="")
    for day in range(1, lastDay + 1):
        print(f" {day}", end="")
    print()

I wouldn't try to pass that off as your own work (you'll almost certainly be pinged for plagiarism) but it's a good bit of code for getting some ideas.

Upvotes: 2

Joran Beasley
Joran Beasley

Reputation: 114038

I will advise you to simply use the built-in calendar module

# Program to display calendar of the given month and year

# importing calendar module
import calendar

yy = 2014  # year
mm = 11    # month

# To take month and year input from the user
# yy = int(input("Enter year: "))
# mm = int(input("Enter month: "))

# display the calendar
print(calendar.month(yy, mm))

# output
#   November 2014
#Mo Tu We Th Fr Sa Su
#                1  2
# 3  4  5  6  7  8  9
#10 11 12 13 14 15 16
#17 18 19 20 21 22 23
#24 25 26 27 28 29 30

Upvotes: 3

Related Questions