Making a random selection from a list of links based on input in a second elif statement in a function?

Advertisements

Python newbie here, and I’m having some trouble with a little beginners project I’m trying to make (also, apologies if the title of the question isn’t very explanatory, I’m still learning the lingo).

The gist of the project is that based on the user’s input of what "mood" they’re feeling, the program will return a YouTube video of music based on that input. For example in the code below, I started with a prompt called "jazzy", which will return jazz music:

import random
import webbrowser

jazzy = [["https://www.youtube.com/watch?v=fAi7IeJG-6Y"], ["https://www.youtube.com/watch?v=bcp2MFfF_xc&t=1686s"], ["https://www.youtube.com/watch?v=eZAuY5M7J-Y"]]
funky = [["https://www.youtube.com/watch?v=ZPHoVmBIqPk"], ["https://www.youtube.com/watch?v=mVphcpIoTpc&t=940s"]]

def user_mood():
    mood = input('What mood are we in today?')
    if mood == "jazzy":
        jazz_open = webbrowser.open_new(random.choice(jazzy))
        jazz_open()
    elif mood == "funky":
        funk_open = webbrowser.open_new(random.choice(funky))
        funk_open()
    elif mood != "jazzy" or "funky":
        print("Sorry, I don't have an option for ", mood, "in my catalogue.")

user_mood()

The "jazzy" section; that is, the if statement portion of the code works perfectly. It randomly chooses from the list in jazzy no problem. However, when I try to input "funky", to get the same process going but for the "funky" list, the program simply closes (using VSCode). Trying it in the windows command prompt returns the last elif statement which says it doesn’t have the input option in the catalogue.

The goal for this little project is to expand the number of genres and the number of options in the lists to a respectable size, but seeing as I’m still very new to Python, I’m not sure if using just the one function for the whole project is a good idea, or if I should split the project into numerous functions for each genre/input.

Thanks a lot in advance! 🙂

>Solution :

The logic here works fine for me. There are a couple of pointers I picked up:

  1. The individual links don’t need to be in lists
  2. webbrowser.open_new(random.choice(funky)) already opens the web browser and returns a boolean, you can’t call the assigned variable after that since it’s not really a function.
  3. The last elif should just be else

If you want to be fancy with it, you can replace the mood lists with a dictionary and call the correct dictionary key without any conditionals for all of the different playlists you want to add.

Here is a modified version:

import random
import webbrowser

playlists = {
    'jazzy': ["https://www.youtube.com/watch?v=fAi7IeJG-6Y", "https://www.youtube.com/watch?v=bcp2MFfF_xc&t=1686s", "https://www.youtube.com/watch?v=eZAuY5M7J-Y"],
    'funky': ["https://www.youtube.com/watch?v=ZPHoVmBIqPk", "https://www.youtube.com/watch?v=mVphcpIoTpc&t=940s"]
}

def user_mood():
    mood = input('What mood are we in today? ').lower()
    if mood not in playlists.keys():
        print("Sorry, I don't have an option for ", mood, "in my catalogue.")
    else:
        webbrowser.open_new(random.choice(playlists[mood]))

user_mood()

Leave a ReplyCancel reply