属性错误:电影实例没有属性'number_of_seasons'



我正在学习用Python编码,但我遇到了一个错误:

AttributeError: Movie instance has no attribute 'number_of_seasons'

所以问题很清楚,但我不知道如何解决。基本上我创建了一些类,我想显示一些具有不同属性的电影和系列。

这是我在其中创建类和属性的 media.py 文件:

class Video():
    """This class created for both common info movies and series"""
    def __init__(self, movie_title, story_line, url_poster, url_trailer, badge):
        self.title = movie_title
        self.storyline = story_line
        self.poster = url_poster
        self.trailer = url_trailer
        self.badge = badge
    def show_trailer(self):
        webbrowser.open(self.trailer)
# here i define the class called Film for all movies
class Movie(Video):
    """This class is used to create all movies"""
    def __init__(self, movie_title, story_line, url_poster, url_trailer, badge, movie_duration, movie_actors):
        Video.__init__(self, movie_title, story_line, url_poster, url_trailer, badge)
        self.duration = movie_duration
        self.actors = movie_actors
# here i define the class called Series for all series with episodes and seasons
class Series(Video):
    def __init__(self, movie_title, story_line, url_poster, url_trailer, badge, number_of_seasons, number_of_episodes):
        Video.__init__(self, movie_title, story_line, url_poster, url_trailer, badge)
        self.number_of_seasons = number_of_seasons
        self.number_of_episodes = number_of_episodes

然后我有enterteinment_center.py文件,我只添加了 1 部电影和 1 部系列:

import media
import fresh_tomatoes

ironman = media.Movie("Ironman",
                      "Genius, billionaire, and playboy Tony Stark, who has inherited the defense contractor Stark Industries from his father",
                      "http://cdn.collider.com/wp-content/uploads/2015/04/iron-man-1-poster.jpg",
                      "https://www.youtube.com/watch?v=8hYlB38asDY",
                      "http://i.imgur.com/45WNQmL.png",
                      "126 minutes",
                      "English")
games_of_thrones = media.Series("Games Of Thrones",
                                "The series is generally praised for what is perceived as a sort of medieval realism.",
                                "https://vignette3.wikia.nocookie.net/gameofthrones/images/2/2c/Season_1_Poster.jpg/revision/latest?cb=20110406150536",
                                "https://www.youtube.com/watch?v=iGp_N3Ir7Do&t",
                                "http://i.imgur.com/45WNQmL.png",
                                "7 Seasons",
                                "12 episodes")
movies = [ironman, games_of_thrones, ironman, games_of_thrones, ironman, games_of_thrones]
fresh_tomatoes.open_movies_page(movies)

创建 HTML 的最后一个文件是fresh_tomatoes.py但我将仅在此处粘贴我认为对修复有用的代码段:

    def create_movie_tiles_content(movies):
    # The HTML content for this section of the page
    content = ''
    for movie in movies:
        if isinstance(movie, Movie):
            # Extract the youtube ID from the url
            youtube_id_match = re.search(r'(?<=v=)[^&#]+', movie.trailer)
            youtube_id_match = youtube_id_match or re.search(r'(?<=be/)[^&#]+', movie.trailer)
            trailer_youtube_id = youtube_id_match.group(0) if youtube_id_match else None
            # Append the tile for the movie with its content filled in
            content += movie_tile_content.format(
                movie_title=movie.title,
                poster_image_url=movie.poster,
                trailer_youtube_id=trailer_youtube_id,
                film_badge=movie.badge,
                film_description=movie.storyline
            )
        elif isinstance(movie, Series):
            # Extract the youtube ID from the url
            youtube_id_match = re.search(r'(?<=v=)[^&#]+', movie.trailer)
            youtube_id_match = youtube_id_match or re.search(r'(?<=be/)[^&#]+', movie.trailer)
            trailer_youtube_id = youtube_id_match.group(0) if youtube_id_match else None
            # Append the tile for the movie with its content filled in
            content += movie_tile_content.format(
                movie_title=movie.title,
                poster_image_url=movie.poster,
                trailer_youtube_id=trailer_youtube_id,
                film_badge=movie.badge,
                film_description=movie.storyline,
                serie_season=movie.number_of_seasons
            )
    return content
def open_movies_page(movies):
  # Create or overwrite the output file
  output_file = open('fresh_tomatoes.html', 'w')
  # Replace the placeholder for the movie tiles with the actual dynamically generated content
  rendered_content = main_page_content.format(movie_tiles=create_movie_tiles_content(movies))
  # Output the file
  output_file.write(main_page_head + rendered_content)
  output_file.close()
  # open the output file in the browser
  url = os.path.abspath(output_file.name)
  webbrowser.open('file://' + url, new=2) # open in a new tab, if possible

所以基本上电影和连续剧有一些不同的属性,所以如果我想显示例如权力的游戏的季节数,我会得到一个错误,告诉电影铁人没有任何称为季节数的属性。

希望有人能在这方面提供帮助! 非常感谢!

不能使用相同的函数为具有不同属性的类提取值。

你需要为每个类中的每个属性分配一个默认值,或者更改函数以检查循环中movie的类型。

例如:

for movie in movies:
    if isinstance(movie, Movie):
        # add movie attributes to the content
        content += movie_tile_content.format(
            movie_title=movie.title,
            poster_image_url=movie.poster,
            trailer_youtube_id=trailer_youtube_id,
            film_badge=movie.badge,
            film_description=movie.storyline,
            serie_seasons='no season'
            )
    elif isinstance(movie, Series):
        # add series attributes to the content
        content += movie_tile_content.format(
            movie_title=movie.title,
            poster_image_url=movie.poster,
            trailer_youtube_id=trailer_youtube_id,
            film_badge=movie.badge,
            film_description=movie.storyline,
            serie_seasons=movie.number_of_seasons
            )    

此模式允许您根据媒体/视频的类型更改内容。

您可能需要导入第二个文件中的类型:

from media import Movie, Series

相关内容

最新更新