Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def obtainFile():
- # MJK I'm not keen on arbitrary flags to control flow.
- # For me a big feature of functions is the ability to exit at any point when a return condition is found.
- # (I admit, not everyone agrees with this! But for me it makes code shorter and clearer).
- # Here I use break to exit the loop, then we can return
- while True: # Keep looping until a valid file is given
- inFile = input('Please enter the name of the data file: ')
- # MJK I think it is worth adding a check that the file entered is a .csv filw
- if not inFile.lower().endswith('.csv'):
- print("Must be a .csv file, try again ...")
- continue
- try:
- newFile = open(inFile, 'r')
- break
- except IOError:
- print("Invalid file name, try again ...")
- return newFile # MJK Exit from function when valid file name is input
- def getData():
- yearList = []
- totalList = []
- menList = []
- womenList = []
- inFile = obtainFile() # MJK Get the file handle of the .csv file
- lineReader = inFile.readline() # MJK Read and skip the header row
- # MJK Read data rows of the .csv file
- for lineReader in inFile:
- fields = lineReader.split(',') # MJK read one row and split into fields
- if len(fields) < 4:
- continue # expecting at least 4 columns per row
- # get these 4 columns individually, as we are not sure how many columns actually exist in the .csv
- year, total, men, women = fields[0], fields[1], fields[2], fields[3]
- # Note: Probably a good idea to use .strip() on each column data
- # We could put these lines within a try: in case of any bad data
- yearList.append(int(year.strip()))
- totalList.append(int(total.strip()))
- menList.append(int(men.strip()))
- womenList.append(int(women.strip()))
- inFile.close()
- return yearList, totalList, menList, womenList # Returning all data (same func for Q1 and Q2)
- # MJK New utility function to get any integer from user input
- def getInt(prompt):
- while True: # Loop until an integer is entered
- user_in = input(prompt)
- if user_in.isdigit(): # check that the input consists of digits only
- break
- print("Non-numeric input. Try again\n")
- return int(user_in) # Return integer value when an integer is input
- # MJK Here is a function to get a year within the required range (range as defined by the .CSV file data itself)
- # yearList is the full list of years extracted from the CSV file
- # For Question 2, year must be between earliestYear+1, and latestYear, inclusive
- # This is better than using a fixed range of 1972 to 2011 inclusive
- def getYear(prompt, yearList, question):
- # Find the minimum year, and the maximum year, from the list of years in the CSV file
- minYear = min(yearList)
- if question == 2: # for Q2, minimum allowed year is earliest year +1
- minYear += 1
- maxYear = max(yearList) # Get the latest year from the CSV
- while True:
- y = getInt(prompt) # Get a valid integer from user input
- if minYear <= y <= maxYear: # Check user year within required range
- break
- print("Year out of range. Try again\n")
- return y # Exit when a valid year is given
- # This function is for Question 1 only
- # This returns 2 years, with the 2nd being greater than the first
- # yearList is the complete list of years found in the CSV file
- def getYears(yearList, question):
- while True:
- year1 = getYear("Enter earlier of two years: ", yearList, question)
- year2 = getYear("Enter later of two years: ", yearList, question)
- if year2 > year1:
- break
- print("2nd year not greater than first. Try again\n")
- return year1, year2
- def computePercentChange(yearList, year1, womenList):
- # storeFileName = ("womenGradChangeFrom" + str(year1) + ".csv")
- # MJK use f-string formatting is nicer
- storeFileName = f"womenGradChangeFrom{year1}.csv"
- f = open(storeFileName, "w")
- chosenYearIndex = yearList.index(year1) # position of chosen year in list
- for i in range(chosenYearIndex, len(yearList)):
- difference = (womenList[i] - womenList[i - 1]) / womenList[i - 1] #
- # MJK NOTE: They have asked for a percentage difference, so multiple ratio by 100
- percentDiff = 100 * difference
- # f.write(str((yearList[i])) + ',' + str(percentDiff) + '\n')
- # MJK Again, f-string formatting is nice
- f.write(f"{yearList[i]},{percentDiff}\n")
- f.close()
- print("Your data has been written to the file " + storeFileName)
- def main():
- # MJK: Use the same function for Q1 and Q2 for getting data
- yearList, totalList, menList, womenList = getData()
- year1 = getYear("Enter a year: ", yearList, 2) # Question 2
- computePercentChange(yearList, year1, womenList)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement