-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent_mark.py
More file actions
65 lines (47 loc) · 1.62 KB
/
Copy pathstudent_mark.py
File metadata and controls
65 lines (47 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def read_file(fname):
try:
f = open(fname, "r")
lines = f.readlines()
f.close()
if len(lines) == 0:
raise ValueError("File is empty.")
total = 0
valid = 0
invalid = 0
print("\nStudent Marks\n")
for i, line in enumerate(lines, start=1):
line = line.strip()
if line == "":
invalid += 1
print("Line", i, ": Invalid (empty line)")
continue
try:
parts = line.split(",")
if len(parts) != 2:
raise ValueError("Incorrect format")
name = parts[0].strip()
mark_text = parts[1].strip()
if name == "":
raise ValueError("Name missing")
marks = int(mark_text)
if marks < 0 or marks > 100:
raise ValueError("Marks out of range")
print(name, "-", marks)
total += marks
valid += 1
except ValueError:
invalid += 1
print("Line", i, ": Invalid record ->", line)
if valid == 0:
print("\nNo valid records found. Average cannot be calculated.")
else:
avg = total / valid
print("\nAverage marks:", round(avg, 2))
print("\nValid records:", valid)
print("Invalid records:", invalid)
except FileNotFoundError:
print("Error: File not found.")
except ValueError as e:
print("Error:", e)
fname = input("Enter file name: ")
read_file(fname)