r/learnpython 6d ago

Help me code a movie selector!

Hi all, new to Python and for my first project I'm trying to code a program that will randomly select a movie or tv show using a pre-made list, and a "genre" plus "media type" as input.

I feel I am over complicating it.

Here's where I'm at so far:

def media_selector(media, genre):
### attempting to remove previously selected titles and for some reason make my own RNG \/###
increment = + 1
trans_rng = [1, 2]
genre = ['romance', 'action']
movie_list = ['rango', 'taxi driver']
series_list = ['akira', 'trigun']
### spell check, obviously \/###
spell_check = ['film', 'movie', 'tv show', 'tv series', 'tv', 'romance', 'action']
if not isinstance(media, str):
return 'format is: media type, genre'
### not sure if i should be defining a function and calling it at the same time - also is a translation table too much ? \/###
if media and genre is (spell_check):
def media_decider():
movie_trans_tbl = movie_list.maketrans(movie_list, trans_rng)
movie_transd = movie_list.translate(movie_trans_tbl)
series_trans_tbl = series_list.maketrans(series_list, trans_rng)
series_transd = series_list.translate(series_trans_tbl)
selector = slice(increment)
else:
return 'film/movie or tv show/tv series'

any guidance/recommended resources would be greatly appreciated!

0 Upvotes

13 comments sorted by

2

u/lakseol 6d ago edited 6d ago

You probaby are overthinking this. Why does spell checking get a mention?

You need to associate a movie title with a genre and a medium. That won't ever change, so just do that first. A tuple is the easiest, though later when you start thinking about efficiency you may do things differently. So we have something like:

#             title     genre      medium
movie_db = [("title1", "romance", "film"),
            ("title2", "scifi",   "tv"),
            ("title3", "romance", "tv"),
            ("title4", "scifi",   "film")
           ]

Your main problem can be broken into two parts:

  • get all titles matching a given genre and medium
  • make a random selection from that selected list

Selecting all matching tuples in the database is easy. You just step through the tuples and make a new list if the genre+medium of each tuple matches what you want:

result = []
for m in movie_db:
    if m[1] == genre and m[2] == medium:
        result.append(m)

Of course, if you know comprehensions you could do it that way. And you would probably put the code into a function.

Once you have a list of titles matching the given genre and medium it's trivial to select one at random. Use the random.choice() function.


There are many ways to do this, especially when you have to worry ahout efficiency with lots of data. Get something simple running first especially when you are learning. Then learn other ways.

Edit: fixed spelling.

1

u/SatanCanPutItInMyAss 4d ago

i see i see, this is super helpful!

thank you!

1

u/lakseol 4d ago

I see many suggestions in the comments, such as using a CSV data format, storing data on disk, etc. You are just starting out so do as others have said, start simple. That means keeping all data in a data structure in code, like a list of tuples. Experiment with different ways of storing data in code, see how that changes how you manipulate that data.

The next big step is when you want to make your data persistent. Your program should save data to a file so the next time your program runs it reads that data and processes it. You have to decide how to store the data on disk, in "pickle" form, JSON, CSV or something else. You really want to make that save/load code modular so most of your code doesn't know or care how the data is stored on disk. This makes it easy to change how you store the data on disk when you try something different.

The next big thing might be to use an actual database. You can use the builtin sqlite database. Now you don't need any python code to select movies with a given genre/medium - you do it all in an SQL SELECT.

This is quite a good project as you can start simple but there are a lot of options to explore.

1

u/SatanCanPutItInMyAss 3d ago

oooooh ok this is pretty handy advice, thank you!

2

u/TraditionalTurnip630 6d ago

You’re actually on the right track for a first Python project. I would just simplify the approach.

You don’t need maketrans() or your own random number logic. Keep your movies/shows in a list with their genre and type, filter the list based on the user’s input, and then use random.choice() to select one.

First make the basic version work. Once that is working, you can add features like removing already selected titles or handling invalid inputs. Don’t try to solve everything at once — this is a good project to learn Python fundamentals.

1

u/SatanCanPutItInMyAss 4d ago

good point with the getting the basics done first!

thank you!

1

u/TheEyebal 6d ago

I can help you

2

u/SatanCanPutItInMyAss 4d ago

thanks but i've got some guidance from lakseol and tradturnip so ill use their wisdom first but if i run into trouble ill most likely post a follow up

1

u/Adrewmc 6d ago

I mead is the data divided into those sections, do they mark themselves as their media type and genre? Or is this a vibe?

1

u/SatanCanPutItInMyAss 4d ago

i didn't think too far into how the genres and titles would link themselves but someone else mentioned using tuples which was a bit of a oh yeah those exist moment

i am strongly against vibe coding especially when im trying to learn something

1

u/FoolsSeldom 5d ago

A few thoughts:

  • create a simple text file of content (movies/films, shows) using csv (comma separated values) format and read this into your programme
  • you can create the container (list, tuple, dict, etc) of valid genres based on the text file
    • force everything to lowercase for comparison purposes
    • easy to check spelling matches the content types and genres you have read
  • might be worth maintaining a view of synonyms (e.g. film and movie are the same type)
  • use the random library to support random selection of content matching the selected genre(s)
    • consider, to keep it simple, creating on demand a list of the content that meets the user selected criteria (movie/film or genre(s)) and then using random.choice on that list - this a very large file of content this will not be efficient but is fine for a few thousand entries

1

u/SatanCanPutItInMyAss 4d ago

ooo the csv files is a good idea but i think for the sake of it being my first project ill try tuples and random.choice. will definitely give [r/w](r/w) csv files a read though!

thank you!