I have created a juke box and have imported a random module. The user picks what
ID: 3588277 • Letter: I
Question
I have created a juke box and have imported a random module. The user picks what genre of music they want(in this particular portion of code it is c = country) then they choose 1-3 of which song they want. I want the fourth option to be a random song. How do I do this?
if genre == "c":
print(" Below is a list of country songs. Please choose which number you would like to play.")
print(" 1. " + songList[0])
print("2. " + songList[1])
song = input("3. " + songList[2] + " ")
if song == "1":
webbrowser.open('https://www.youtube.com/watch?v=rB7ONnfIjaI')
elif song == "2":
webbrowser.open('https://www.youtube.com/watch?v=pl4c0kZUm2M')
elif song == "3":
webbrowser.open('https://www.youtube.com/watch?v=vNVguvNE7qc')
elif song == "4":
?????????????????????????????
else:
print(" Sorry " + song + " is not one of the choices.")
Explanation / Answer
Put above code in the place of ?????????????????????????????.
if genre == "c":
print(" Below is a list of country songs. Please choose which number you would like to play.")
print(" 1. " + songList[0])
print("2. " + songList[1])
song = input("3. " + songList[2] + " ")
if song == "1":
webbrowser.open('https://www.youtube.com/watch?v=rB7ONnfIjaI')
elif song == "2":
webbrowser.open('https://www.youtube.com/watch?v=pl4c0kZUm2M')
elif song == "3":
webbrowser.open('https://www.youtube.com/watch?v=vNVguvNE7qc')
elif song == "4":
import random
a = ['https://www.youtube.com/watch?v=rB7ONnfIjaI', 'https://www.youtube.com/watch?v=pl4c0kZUm2M', 'https://www.youtube.com/watch?v=vNVguvNE7qc']
r = random.randint(0,2)
webbrowser.open(a[r])
else:
print(" Sorry " + song + " is not one of the choices.")
a = ['https://www.youtube.com/watch?v=rB7ONnfIjaI', 'https://www.youtube.com/watch?v=pl4c0kZUm2M', 'https://www.youtube.com/watch?v=vNVguvNE7qc']
r = random.randint(0,2)
webbrowser.open(a[r])
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.