Sep-17-2017, 11:01 PM
This is my updated code and it only prints out the last line in the input file which is "6464". Any ideas what I need to change? I can't use a split() function in my assignment.
# function 4 def leapYear(year): """ Calculates whether a year is a leap year or not. """ year = int(year) return year % 4 == 0 and (year % 10 != 0 or year % 400 == 0) # function 3 def writeFile(): """ Opens, writes to, and closes output file. """ file_2 = open(output_file, "w") # open output file file_2.write(checkRawData()) # write to output file file_2.close() # function 2 def checkRawData(): """ Checks raw data by looping through conditions. """ for line in readFile(): # loop through input file if line[:4].isdigit(): if leapYear(line[:4]): filtered_data = line[:4] return filtered_data # function 1 def readFile(): """ Opens, reads and closes input file. """ try: file_1 = open(input_file, "r") # open input file lines = file_1.readlines() # read input file file_1.close() except ValueError as e: print(e) return lines # user input input_file = input("Enter the name of the input file you want to read: ") output_file = input("Enter the name of the output file to which you want to write: ") # function calls readFile() checkRawData() writeFile()