Python Forum
random selection of song and artist not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random selection of song and artist not working
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Menu Choice Selection not Working ChLenx79 5 1,535 Nov-23-2022, 05:56 AM
Last Post: deanhystad
  Can I include text using artist? tetrisbot 0 1,430 Aug-13-2020, 08:13 PM
Last Post: tetrisbot
  Song Modifying Program Starting Point BadenJaden 1 1,868 Mar-27-2020, 11:36 AM
Last Post: pyzyx3qwerty
  random.uniform is not working correctly dron4ik86 9 4,107 Feb-06-2020, 03:04 PM
Last Post: dron4ik86
  Finding MINIMUM number in a random list is not working Mona 5 3,041 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  splitting a random song Unknown_Relic 3 2,332 Nov-15-2019, 01:04 PM
Last Post: baquerik
  Working with Random Generated Numbers YoungGrassHopper 4 3,205 Sep-10-2019, 06:53 AM
Last Post: YoungGrassHopper
  Random selection EMarburg 6 2,995 Aug-12-2019, 01:15 AM
Last Post: ichabod801
  How to play a song .wav or .mp3 with audioop native library IvanSilva 3 6,536 Mar-14-2018, 10:49 AM
Last Post: snippsat
  Random number selection stumunro 4 3,631 Aug-31-2017, 02:12 PM
Last Post: stumunro

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020