Ask coding questions

← Back to all posts
Python CodeHS Question
SimplyMiah (1)

Hey guys! I’m currently writing a code that gives song suggestions if someone simply presses a key, but I’m stuck on how to use the list I made in my code, which is my_songs[ blah, blah, blah]. Can anybody please help me? Thank you!

Comments
hotnewtop
IMayBeMe (456)

One way to do this is to create a function which is called on key press the function can look like this:

import random
my_songs = [song1, song2, song3]
get_song = lambda: random.choice(my_songs)
SimplyMiah (1)

@IMayBeMe wait..so if I made a list song_options([‘blah’,’blah’,’blah]) and before that made the def function def song_options (input) : , would that be considered using my list function?

InvisibleOne (2934)

Do you want a random item from the list?

import random
songs = ['bla bla bla', 'bla bla', 'bla bla bla bla']
while True:
  input("Press enter to generate a song!")
  print(random.choice(songs))

Or just select them from the list, with their index number

songSelected = songs[index_number]
VulcanWM (2758)

yeah and also to get the index number of an item do:

index = songs.index("song name") 

@InvisibleOne

SimplyMiah (1)

@InvisibleOne wait..so if I made a list song_options([‘blah’,’blah’,’blah]) and before that made the def function def song_options (input) : , would that be considered using my list function?