Answer :
If the file name is invalid, you will have three attempts to enter a valid file name before the program quits.
Here's a Python program that implements the requirements you mentioned:
```python
import os
def validate_file_name(file_name):
return os.path.isfile(file_name)
def read_student_data(file_name):
students = {}
current_student = None
with open(file_name, 'r') as file:
for line in file:
line = line.strip()
if line.startswith('Student'):
student_name = line.split(' ')[1]
current_student = student_name
students[current_student] = []
else:
grade = float(line)
students[current_student].append(grade)
return students
def calculate_average(grades):
return sum(grades) / len(grades)
def display_student_data(students):
for student, grades in students.items():
grade_average = calculate_average(grades)
grade_list = ' '.join(str(grade) for grade in grades)
print(f"{student} {grade_list} {grade_average}")
def main():
attempts = 0
while attempts < 3:
file_name = input("Enter the input file name: ")
if validate_file_name(file_name):
students = read_student_data(file_name)
display_student_data(students)
break
else:
print("Invalid file name. Please try again.")
attempts += 1
if attempts == 3:
print("Exceeded maximum number of attempts. Exiting program.")
if __name__ == '__main__':
main()
```
Make sure to save the above code in a `.py` file, such as `assignment4.py`. When you run the program, it will ask you to enter the input file name. If you enter a valid file name (an existing file), it will read the student information from the file, display each student's grades along with the average. If the file name is invalid, you will have three attempts to enter a valid file name before the program quits.
Note: Please replace the necessary information in the header of the program before submitting it.
To know more about Programming related question visit:
https://brainly.com/question/14368396
#SPJ11