Home > Enterprise >  Making a random selection from a list of links based on input in a second elif statement in a functi
Making a random selection from a list of links based on input in a second elif statement in a functi

Time:09-22

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

CodePudding user response:

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()

CodePudding user response:

Your problem is with your last elif-statement. what you want is to check if mood is neither jazzy nor funky.

replace

elif mood != "jazzy" or "funky"

with

elif mood not in ("jazzy", "funky"):

CodePudding user response:

The code is looking good except I know I can spot one thing that you want to change at least! Currently the last statement will always execute, because the compiler will look at the expression like this and a string in the expression is always true:

(mood != "jazzy") or ("funky")

Since you actually don't even wanna check anything for the last case and you always want to execute the last print if the first 2 if-statements are false, you can replace everything with 'else':

else:
    print("Sorry, I don't have an option for ", mood, "in my catalogue.")

CodePudding user response:

You have assigned a list of list in jazzy and funky. You should assign simple string of url:

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"]

You dont have to call the function of junk_open() and fuzzy_open(). The statement

webbrowser.open_new(url)

opens a webbrowser and plays the url.

And yes last block of if-elif-else ladder should be generally else instead of if-elif-elif.

Rest things are good. Hope this solves the issue. Happy learning!

  • Related