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.