Python Forum

Full Version: random selection of song and artist not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
im trying to select a random song in a file then find the position and match it with the artist in a separate file in python 3.7.3. Two files called songnames, songartists. Im want to print the song and artist but i keep getting a value error. Im new to python and not sure how to fix it.
import random

def access_data():
    with open("Passwords.txt", "r") as f:
        passwords = [line.rstrip("\n") for line in f]
    with open("Usernames.txt", "r") as f:
        usernames = [line.rstrip("\n") for line in f]
    with open("Songnames.txt", "r") as f:
        songnames = [line.strip("\n") for line in f]
    with open("songartists.txt", "r") as f:
        songartists = [line.strip("\n") for line in f]
    return passwords, usernames, songnames, songartists


def song_selection(songnames, songartists):
    random_song = random.choice(songnames)
    random_songPos = songnames.index(random_song)
    random_artist = songartists[random_songPos]
    print(random_song, random_artist)
    return random_song, random_artist

songnames, songartists, songnames, songartists = access_data()
song_selection(songnames, songartists)
What is the full text of the error? I would expect an index error, not a value error. Also, are you sure the two lists match up. What are len(songnames) and len(songartists)? Finally, you might want to look at line 22 more carefully. I don't think that's the problem, but I don't think it's what you intended either.
In addition to ichabod801: is it part of longer code? Otherwise why open and read data from passwords and usernames files? This data is not used in this snippet.