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
+1 vote
172 views
in Python by (120 points)
There are 20 text files whose names are equal to 1 to 20, containing movie information, in the order of movie name, director's name, year of production. Create a class called movie, whose fields can store all the movie information that is in the text file. 
In addition, the movie score can be saved in a field. In this class, if we want to print the name of an object, at the time of printing, the name and year of production associated with the object will be printed.

Please log in or register to answer this question.

1 Answer

0 votes
by (41.7k points)

Python class Movie that stores movie information from the text files and includes the functionality you've described:

class Movie:
    def __init__(self, name, director, year):
        self.name = name
        self.director = director
        self.year = year
        self.score = None

    def set_score(self, score):
        self.score = score

    def __str__(self):
        return f"Movie: {self.name} ({self.year})"
# Reading movie information from text files and creating Movie objects
movies = []
for i in range(1, 21):
    filename = f"{i}.txt"
    with open(filename, 'r') as file:
        lines = file.readlines()
        name = lines[0].strip()
        director = lines[1].strip()
        year = int(lines[2].strip())
        
        movie = Movie(name, director, year)
        movies.append(movie)

# Setting scores for the movies (you can replace these with actual scores)
for i, movie in enumerate(movies):
    movie.set_score(i * 0.5)

# Printing movie names along with the associated year of production
for movie in movies:
    print(movie)

This code assumes you have text files named "1.txt" through "20.txt," with each containing the movie name, director's name, and year of production on separate lines. The code reads the information from these text files, creates Movie objects, sets scores, and then prints the movie names along with the associated year of production, as you've described.

Related questions

0 votes
1 answer
asked Aug 19, 2023 in Python by MAhmad (120 points)
0 votes
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)
+1 vote
1 answer
asked Dec 21, 2020 in Binomial theorem by Atharv Inamdar (235 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

...