Python3 How can I improve my code follow the instruction?
Question
Python3
How can I improve my code follow the instruction? In self. part in line6 to line10. I want to remake my code in the specified format:
__title (string)
__release_year (integer)
__director (string)
__ratings (list) Hint: initialize to empty list.
__average_rating (float) Hint: initialize to 0.
movie.py:
class Movie: # Define attributes and methods for a Movie class, # as specified in the instructions # The attributes should be PRIVATE def _init_(self, title, release_year, director, ratings = [], avg_rating=0): self.__title = title self.__release_year = release_year self.__director = director self.__ratings = ratings self.__average_rating = avg_rating def _str_(self): return ("Title: "+self._title + " Year: " + str(self.release_year) + " Director: " + self._director) def add_rating(self, rating): self.__ratings.append(rating) def calc_average_rating(self): self._average_rating = sum(self.ratings) / len(self._ratings) def getRatings(self): return self.__ratings def getAverageRating(self): return self.__average_rating
Summary
Here in this scenario, we have to remake a code in the specific format which is coded in the language Python. So we have only a certain datatype that is assigned to the movie class which has the attribute in the constructor. So each specific datatype value is to be initialized by the constructor where each argument is passed.
Explanation
In this program, we have a constructor from line 6 to line 10, in this constructor, the header of each argument i.e. passed with a specified value is also assigned as a default argument. So to have the title value there is an empty string with a string datatype, where we have passed the value to the str function before assigning it to the title attribute and next we have done this process with the remaining arguments.
Because we have assigned the integer datatype to release_year, a string data type to a director, a list datatype to ratings, and finally a float datatype to an average_rating.
As all the attributes are private, to return the type of each attribute datatype method is defined.
class Movie: """ having default arguments so that the type is set already """ def __init__(self,title="",release_year=2020,director="",ratings=[],avg_rating=0.0): self.__title=str(title) self.__release_year=int(release_year) self.__director=str(director) self.__ratings=list(ratings) self.__avg_rating=float(avg_rating) def __str__(self): return ("Title: "+self.__title+" Year: "+str(self.__release_year)+" Director: "+ director+"\n") def add_rating(self,rating): self.__ratings.append(rating) def calc_average_rating(self): self.__average_rating=sum(self.__ratings)/len(self.__ratings) def getRatings(self): return self.__ratings def getAverageRating(self): return self.__average_rating """ to return the type of the title """ def type_title(self): return type(self.__title) """ to return the type of the release_year """ def type_year(self): return type(self.__release_year) """ to return the type of the director """ def type_dir(self): return type(self.__director) """ to return the type of the ratings """ def type_rat(self): return type(self.__ratings) """ to return the type of the average rating """ def type_avg(self): return type(self.__avg_rating) m = Movie() print("Types of each attribute of Movie class:") print("title type:",m.type_title()) print("release_year type:",m.type_year()) print("director type:",m.type_dir()) print("ratings type:",m.type_rat()) print("avg_rating type:",m.type_avg())
Output