Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
137 views
in Python by (120 points)
Twenty text files, whose names are equal to 1 to 20, contain movie information, arranged in the order of movie name, director's name, year of production.Create a class called movie, which stores all the information in the text file in its fields. In addition, the movie score can be stored in a field. In this class, if we want to print the name of an object, the name and year of production related to it will be printed at the time of printing.

Please log in or register to answer this question.

1 Answer

0 votes
by (41.7k points)

Python class named Movie that fulfills your requirements:

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}), Director: {self.director}"

# 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 information
for movie in movies:
    print(movie)
    if movie.score is not None:
        print(f"Score: {movie.score}")
    print("-" * 30)

Replace the placeholder scores (i * 0.5) with actual scores if you have them. This code assumes that you have text files named "1.txt" through "20.txt", 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 information as you described.

Related questions

+1 vote
1 answer
asked Aug 18, 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
0 votes
1 answer
+1 vote
1 answer

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

...