Sep-16-2017, 10:09 PM
(This post was last modified: Sep-16-2017, 10:13 PM by forumer444.)
This is my update if anyone can help me finish it.
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?
# 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?