The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a music playlist generator

Here’s a simple music playlist generator implemented in Python. It allows users to input a mood or genre, and the generator creates a playlist by randomly selecting songs from predefined categories.

python
import random # Sample song database categorized by mood and genre song_database = { "happy": [ "Happy – Pharrell Williams", "Can't Stop the Feeling! – Justin Timberlake", "Walking on Sunshine – Katrina & The Waves", "Good as Hell – Lizzo", "Best Day of My Life – American Authors" ], "sad": [ "Someone Like You – Adele", "Let Her Go – Passenger", "Fix You – Coldplay", "Say Something – A Great Big World", "Tears Dry on Their Own – Amy Winehouse" ], "relaxing": [ "Weightless – Marconi Union", "Sunset Lover – Petit Biscuit", "Breathe Me – Sia", "Holocene – Bon Iver", "Skinny Love – Birdy" ], "energetic": [ "Eye of the Tiger – Survivor", "Stronger – Kanye West", "Uptown Funk – Bruno Mars", "Don't Start Now – Dua Lipa", "Thunder – Imagine Dragons" ], "rock": [ "Smells Like Teen Spirit – Nirvana", "Bohemian Rhapsody – Queen", "Sweet Child O' Mine – Guns N' Roses", "Hotel California – Eagles", "Back in Black – AC/DC" ], "pop": [ "Blinding Lights – The Weeknd", "Levitating – Dua Lipa", "Bad Guy – Billie Eilish", "Shake It Off – Taylor Swift", "As It Was – Harry Styles" ] } def generate_playlist(category, number_of_songs=5): category = category.lower() if category not in song_database: return f"Category '{category}' not found. Available categories: {', '.join(song_database.keys())}" playlist = random.sample(song_database[category], min(number_of_songs, len(song_database[category]))) return f"🎵 Playlist for '{category.title()}' mood/genre:n" + "n".join(f"{i+1}. {song}" for i, song in enumerate(playlist)) # Example usage: if __name__ == "__main__": user_input = input("Enter a mood or genre (e.g., happy, sad, relaxing, energetic, rock, pop): ") try: num_songs = int(input("How many songs would you like in the playlist? ")) except ValueError: num_songs = 5 # Default print(generate_playlist(user_input, num_songs))

Features:

  • Categorizes songs by mood/genre.

  • Randomly picks a specified number of songs.

  • Easily expandable with more songs or categories.

You can modify this script for a GUI version using Tkinter or integrate it into a web app using Flask or Django. Would you like a version for a web app or mobile app next?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About