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

Time:02-04

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.

CodePudding user response:

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]))
  • Related