SimeonTs

SUPyF2 Objects/Classes-Exericse - 09. Movie

Oct 19th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. """
  2. Objects and Classes - Exericse
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1734#8
  4.  
  5. SUPyF2 Objects/Classes-Exericse - 09. Movie (not included in final score)
  6.  
  7. Problem:
  8. Create a class Movie. The __init__ method should receive a name and director.
  9. It should also have default value of an attribute watched to be False.
  10. There should also be a class attribute __watched_movies which will keep track of all the watched movies.
  11. The class should have the following methods:
  12. • change_name(new_name) - changes the name of the movie
  13. • change_director(new_director) - changes the director of the movie
  14. • watch() - change the watched attribute to True and increase the total watched movies class attribute
  15.    (if the movie is not already watched)
  16. • __repr__() - returns "Movie name: {name}; Movie director: {director}. Total watched movies: {__wached_movies}"
  17.  
  18. Examples:
  19.    Test Code:
  20. first_movie = Movie("Inception", "Christopher Nolan")
  21. second_movie = Movie("The Matrix", "The Wachowskis")
  22. third_movie = Movie("The Predator", "Shane Black")
  23. first_movie.change_director("Me")
  24. third_movie.change_name("My Movie")
  25. first_movie.watch()
  26. third_movie.watch()
  27. first_movie.watch()
  28. print(first_movie)
  29. print(second_movie)
  30. print(third_movie)
  31.  
  32.    Output:
  33. Movie name: Inception; Movie director: Me. Total watched movies: 2
  34. Movie name: The Matrix; Movie director: The Wachowskis. Total watched movies: 2
  35. Movie name: My Movie; Movie director: Shane Black. Total watched movies: 2
  36. """
  37.  
  38.  
  39. class Movie:
  40.     __watched_movies = 0
  41.  
  42.     def __init__(self, name: str, director: str):
  43.         self.name = name
  44.         self.director = director
  45.         self.watched = False
  46.  
  47.     def change_name(self, new_name):
  48.         self.name = new_name
  49.  
  50.     def change_director(self, new_director):
  51.         self.director = new_director
  52.  
  53.     def watch(self):
  54.         if not self.watched:
  55.             self.watched = True
  56.             Movie.__watched_movies += 1
  57.  
  58.     def __repr__(self):
  59.         return f"Movie name: {self.name}; Movie director: {self.director}. Total watched movies: {Movie.__watched_movies}"
  60.  
  61.  
  62. first_movie = Movie("Inception", "Christopher Nolan")
  63. second_movie = Movie("The Matrix", "The Wachowskis")
  64. third_movie = Movie("The Predator", "Shane Black")
  65. first_movie.change_director("Me")
  66. third_movie.change_name("My Movie")
  67. first_movie.watch()
  68. third_movie.watch()
  69. first_movie.watch()
  70. print(first_movie)
  71. print(second_movie)
  72. print(third_movie)
Advertisement
Add Comment
Please, Sign In to add comment