jeudi 22 septembre 2022

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

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! :)




Aucun commentaire:

Enregistrer un commentaire