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?
#10
This is my update if anyone can help me finish it.

# calculation function
def leapYear(year):
    """ Calculates whether a year is or isn't a leap year. """
    year = int(year)
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

# main function
def main():
    try:
        file_1 = open("all_years.txt", "r") # open file
        lines = file_1.readlines()
        file_1.close()

        file_2 = open("leap_years.txt", "a") # open file

        for line in lines:
            if line.isdigit():
                if leapYear(line):
                    file_2.write(line)
        file_2.close()
    except ValueError as e:
        print(e)

main()
It's FINALLY writing to the other file, but only one line which is "6464", which is the very last line in the "all_years.txt" file. Any reasons why?

Quote:The extra text you can probably deal with using the split method of strings. line.split() will break line into a list of words based on white space (spaces and tabs). Then you can check the first word with the isdigit method. word.isdigit() will be true if all the characters in the word are numbers. You just need to watch out for empty lines. The split method will give you an empty list for a blank line.

I believe I'm not a loud to use the split() method, but I used the isdigit method. Can you take a look at my latest post?
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