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.