I’m working on a program that processes student numbers and needs to categorize them as valid or invalid. A student number is considered valid if it has exactly eight digits and contains only numbers. Valid numbers should go into one list, invalid ones into another.
I want to understand how to apply the try except Python structure correctly for this scenario. I’m stuck on how to use it for checking each student number and handling exceptions where the input isn’t valid.
Here’s a simplified version of my current code structure.
Could someone guide me on how to complete the try except sections to handle the validation logic cleanly?
If anyone could help, I would greatly appreciate it!
Valid_file = "ValidNumbers.txt"
Invalid_file = "InvalidNumbers.txt"
Data_file = "Data.txt"
def analyse_students(Data_file):
lenth = len(Data_file)
total = 0
try:
except:
return 0
def read(Data_file):
count = 0
student_list = []
try:
open(Data_file)
except:
print("Couldn't append file", Valid_file)
return count, student_list
def write(student, status):
if status:
try:
open(Data_file)
except:
print("Couldn't append file", Invalid_file)
count, student_list = read(Data_file)
print("Number of lines read", count)
for student in student_list:
print("See output files")