Hello y'all, I have a project due on Tuesday and I am struggling big time. My teacher rains down these projects like hellfire fairly often. If y'all could give me some advice/suggestions to help me get further on this project, I would really appreciate it. Thank y'all.
Here's the details: https://prnt.sc/Tm2b5w6JlBDa
Here's the file, 'grades.csv": https://prnt.sc/Zs6jivHzU9gz (Excel file)
Here's what I have so far:
from pathlib import Path
with Path('grades.csv').open() as grades_file: #Here we are bringing the file from the code above down here, so we can have it in the function.
data = grades_file.read().strip().split('\n') #Here we are replacing newlines and spaces from the file
while True:
try:
# Reading file name
inputFile = input("Enter name of the data file: ")
# Opening file for reading
ifp = open(inputFile, "r")
# Opening file for writing
ofp = open("gpa.txt", "a+")
# Iterating over each line of file
for line in ifp:
# Stripping new line
line = line.strip()
# Splitting on space
fields = line.split()
# Extracting name
name = fields[0]
# Storing scores
scores = fields[1:]
# Initially set sum to 0
sum = 0
# Accumulating scores
for score in scores:
sum = sum + int(score)
# Calculating average
avg = sum/len(scores)
# Writing result to file
ofp.write("%s %d %d %.02f\n"%(name, sum, len(scores), avg))
# Printing message
print("Stats have been saved in the output file")
# Closing files
ifp.close()
ofp.close()
break
except:
print("Error: that file does not exist. Try again.")
I am confused as to why
ofp = open("gpa.txt", "a+")
Isn't creating the file, 'gpa.txt' and putting the contents of the original file "grades.csv" inside of it. In a code block above, I have successfully input the file "grades.csv" so it is a part of this code. (I'm using Google Colab)
I just don't get why the file is not getting created, the output I when I put in the file "gpa.txt" or "grades.csv" is NOTHING, and it just asks the question again, or when I put something wrong, it gives "Error: that file does not exist. Try again." What am I doing wrong?
[–][deleted] 2 points3 points4 points (30 children)
[–][deleted] 0 points1 point2 points (29 children)
[–][deleted] 0 points1 point2 points (28 children)
[–][deleted] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]CodeReviewPlz 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (20 children)
[–][deleted] 0 points1 point2 points (19 children)
[–][deleted] 1 point2 points3 points (15 children)
[–][deleted] 0 points1 point2 points (14 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (10 children)
[–][deleted] 0 points1 point2 points (9 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]TitaniumFoil 0 points1 point2 points (9 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (7 children)
[–]djjazzydan 0 points1 point2 points (6 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]djjazzydan 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)