Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to create a set of lists that after some input from the user one will be randomly selected

I’m completely new to all this, I have no clue what I’m doing. I just want an easy selector that asks "What genre are you feeling?" then depending on if the input is rock, rap, indie, or whatever I add in the future spits back a randomly selected album in a list.

from random import choice

def lists()
    rock_albums = ['Dark Side of the Moon - Pink Floyd']
    rap_albums = ['Igor - Tyler, the Creator']
    indie_albums = ['Currents - Tame Impala']

print("What genre are you feeling?")
genre = input()

if input("rock"):
    rock = random.choice(albums_rock)
    print(rock)

I haven’t gone further to add more albums or any other if statements for other genres.

I don’t know what else to try. I started with one larger list that could be randomly selected from, that worked, so I advanced. Now I’m stuck.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can store all the genres in a dict, then pick from the appropriate list based on the key.

import random
albums = {
    'rock' : ['Dark Side of the Moon - Pink Floyd'],
    'rap' : ['Igor - Tyler, the Creator'],
    'indie' : ['Currents - Tame Impala']
}
genre = input("What genre are you feeling?\n")
if genre in albums:
    print(random.choice(albums[genre]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading