Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
258 views
in Python by (120 points)
edited by
1) Create an object from the Student class for each student. Read each student's profile from the relevant text file and save it inside this object. answer sheet Open each student in order and compare with the correct answer sheet. To Consider 3 points for each correct answer, 1-point for each wrong answer and zero points for unanswered questions. Points for each student Calculate the extra and put it in the field corresponding to his score in the corresponding object. Finally, add each of the objects in a list called exam do.

2) Sort the exam list according to the scores of the students and print the list.

Please log in or register to answer this question.

1 Answer

0 votes
by (41.7k points)

It seems you're describing a scenario involving a class named Student and conducting an exam where you read student profiles and compare their answers to a correct answer sheet to calculate scores. Here's a Python implementation of the scenario you've described:

Assuming the text files for student profiles are named "student1.txt", "student2.txt", ..., and the correct answer sheet is named "correct_answers.txt". Each student's text file should contain their name on the first line, followed by their answers on the subsequent lines.

class Student:
    def __init__(self, name, answers):
        self.name = name
        self.answers = answers
        self.score = 0

    def calculate_score(self, correct_answers):
        for student_ans, correct_ans in zip(self.answers, correct_answers):
            if student_ans == correct_ans:
                self.score += 3
            elif student_ans != '':  # Count only wrong answers, not unanswered questions
                self.score -= 1

    def __str__(self):
        return f"Student: {self.name}, Score: {self.score}"

# Read correct answers from the correct_answers.txt file
with open("correct_answers.txt", 'r') as correct_answers_file:
    correct_answers = correct_answers_file.read().splitlines()

# Create Student objects, read answers from student files, and calculate scores
exam = []
for i in range(1, 21):  # Assuming there are 20 students
    student_filename = f"student{i}.txt"
    with open(student_filename, 'r') as student_file:
        student_name = student_file.readline().strip()
        student_answers = student_file.read().splitlines()
        student = Student(student_name, student_answers)
        student.calculate_score(correct_answers)
        exam.append(student)

# Sort the exam list based on students' scores
exam.sort(key=lambda student: student.score, reverse=True)

# Print the sorted exam list
for student in exam:
    print(student)

This code reads student profiles from text files, compares their answers with the correct answer sheet, calculates scores, and then sorts and prints the list of students based on their scores. Make sure to replace the filenames and content accordingly to match your scenario.

Related questions

0 votes
1 answer
asked Aug 19, 2023 in Python by MAhmad (120 points)
+1 vote
1 answer
asked Aug 18, 2023 in Python by MAhmad (120 points)
0 votes
1 answer
asked Sep 23, 2024 in Physics by manas1423 (20 points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...