Python Forum
What am I doing wrong?
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What am I doing wrong?
#1
This is driving me crazy.

*** Program Description ***
Write a program to read in a file of years, one per line, and output to a separate file "only those years that are in fact leap years" (according to the Gregorian calendar). Ask the user to input the file names of both files, and use the try-except construct when you open each in your program.

# function 1
def leapYear(year):
    """ Calculates whether a year is or isn't a leap year. """
    year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
# function 2
def appendFile():
    file = open("leap_years.txt", "a") # create new - empty - data list
    for line in lines: # loop through raw data
        if leapYear():
            ... idk ...
try:
    file = open("all_years.txt", "r")
    lines = file.readlines()
    file.close()
    
    appendFile()
except FileNotFoundError as e:
    print(e)
I have been working on this for like 20 hours total and am getting nowhere. I have the part down where I read the "all_years.txt" file, but now I need the program to loop through the already read file, calculate if each line is a leap year or not, and write to a new blank file all of the years that are leap years. What am I doing wrong?
Reply


Messages In This Thread
What am I doing wrong? - by forumer444 - Sep-15-2017, 08:39 PM
RE: What am I doing wrong? - by Larz60+ - Sep-15-2017, 09:01 PM
RE: What am I doing wrong? - by ichabod801 - Sep-15-2017, 09:01 PM
RE: What am I doing wrong? - by forumer444 - Sep-15-2017, 09:32 PM
RE: What am I doing wrong? - by ichabod801 - Sep-15-2017, 09:04 PM
RE: What am I doing wrong? - by nilamo - Sep-15-2017, 09:30 PM
RE: What am I doing wrong? - by ichabod801 - Sep-15-2017, 10:59 PM
RE: What am I doing wrong? - by forumer444 - Sep-16-2017, 03:24 AM
RE: What am I doing wrong? - by ichabod801 - Sep-16-2017, 01:24 PM
RE: What am I doing wrong? - by forumer444 - Sep-16-2017, 10:09 PM
RE: What am I doing wrong? - by ichabod801 - Sep-17-2017, 01:27 AM
RE: What am I doing wrong? - by forumer444 - Sep-17-2017, 11:01 PM
RE: What am I doing wrong? - by ichabod801 - Sep-18-2017, 03:21 AM
RE: What am I doing wrong? - by forumer444 - Sep-20-2017, 12:47 AM
RE: What am I doing wrong? - by ichabod801 - Sep-20-2017, 01:24 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020