简体   繁体   中英

How would i convert all these variables into a list. where i could call a random film in python or is there a better way to sort through this?

I am working on a project where I can list my favourite films, then call a random film from the list. Is there a easier way i can do this rather than typing each film out? Here is my code below:

from dataclasses import dataclass import webbrowser import random

class Film():
    def __init__(self, name, genre, rating):
        self.name = name
        self.genre = genre
        self.rating = rating #0-100 
    
    def get_name(self):
        return ("The Name of the film is: " + str(self.name))
    def get_genre(self):
        return (" The Genre of the film is: " + str(self.genre))
    def get_rating(self):
        return (" The rating of the film is: " + str(self.rating))
    def get_namerate(self):
        return self.get_name() + self.get_rating()


    def __str__(self):
        return self.get_name()+ self.get_genre() + self.get_rating()



#horror
h1 = Film("the shining, shining" + "\n", "horror \n ", 76)
h2 = Film("get out \n" ,"horror \n",77)
h3 = Film("midsommer \n","horror \n",74)
h4 = Film("IT \n", "horror \n", 70)
h5 = Film("hereditary\n", "horror \n", 81)
#animation
a1 = Film("up \n", "animation", 82)
a2 = Film("shrek \n", "animation \n", 86)
a3 = Film("Spiderman, into the spider-verse \n", "animation \n", 91)
a4 = Film("zootopio", "animation", 82)
a5 = Film("Monsters inc", "animation", 86)
#Drama
d1 = Film("about time \n", "drama", 87)

you can use a new class which store all the object to each film and can implement all the method to it.

class Films:
    def __init__(self, films):
        self.films = films
    def get_all_films(self.):
         return [ film.__str__() for film in self.films]

films = Films([h1,h2, h3, h4, h5, a1, a2, a3, a4, a5, d1])
print(films.get_all_films())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM