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?
#14
Quote:[*]readFile(), but you don't do anything with the return value.
[*]checkRawData(), which calls readFile() again, and you don't do anything with the returned filtered_data.
[*]writeFile(), which calls checkRawData() again, which calls readFile() a third time.
[/list]
At the end of the file you just need one call to the top level function, currently writeFile.

How does this look now? It's my final edit:

def readFile():
    """ Reads the input file entered by the user. """
    try:
        file_1 = open(input_file, "r")
        lines = file_1.readlines()
        file_1.close()
    except FileNotFoundError as e:
        print(e)

    return lines

def checkRawData():
    """ Checks raw data inside the input file. """
    filtered_data = ""
    
    for line in readFile():
        if line[:4].strip().isdigit():
            if leapYear(line[:4]):
                filtered_data += line[:4] + "\n"

    return filtered_data

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)

def writeFile():
    """ Writes to the output file entered by the user. """
    try:
        file_2 = open(output_file, "w")
        file_2.write(checkRawData())
        file_2.close()
    except FileNotFoundError as e:
        print(e)

# 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()

It works by the way, but is there a better way to structure it?
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