Nov-08-2019, 11:38 AM
(This post was last modified: Nov-08-2019, 11:39 AM by Unknown_Relic.)
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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) |