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.
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)
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
  "Random" module is not working as expected Genericgamemaker 1 980 Jul-25-2024, 04:46 PM
Last Post: Gribouillis
  Random student selection from groups. esahan 7 1,806 Jul-08-2024, 12:28 AM
Last Post: AdamHensley
  Menu Choice Selection not Working ChLenx79 5 3,071 Nov-23-2022, 05:56 AM
Last Post: deanhystad
  Can I include text using artist? tetrisbot 0 1,906 Aug-13-2020, 08:13 PM
Last Post: tetrisbot
  Song Modifying Program Starting Point BadenJaden 1 2,438 Mar-27-2020, 11:36 AM
Last Post: pyzyx3qwerty
  random.uniform is not working correctly dron4ik86 9 6,238 Feb-06-2020, 03:04 PM
Last Post: dron4ik86
  Finding MINIMUM number in a random list is not working Mona 5 4,463 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  splitting a random song Unknown_Relic 3 3,349 Nov-15-2019, 01:04 PM
Last Post: baquerik
  Working with Random Generated Numbers YoungGrassHopper 4 4,981 Sep-10-2019, 06:53 AM
Last Post: YoungGrassHopper
  Random selection EMarburg 6 4,392 Aug-12-2019, 01:15 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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