Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splitting a random song
#1
this is meant to be a music quiz where they login and need to guess the song by the first letters of each word in the title. When im run the code i get an error with the split_song. Im fairly new to python and i cant find 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 login(usernames, passwords):
    enter_username = input("Input your username")
    if enter_username in usernames:
        enter_password = input("Input your password")
        if enter_password in passwords:
            username_pos = usernames.index(enter_username)
            password_pos = passwords.index(enter_password)
            if username_pos == password_pos:
                print("well done you logged in", enter_username)
                return True
            else:
                print("your password does not match the username")
        else:
            print("password not found")
    else:
        print("username is not authorised")
    return False

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_songPos, "/", random_artist)
    return random_song, random_artist

    
def split_song(songnames):
    print(songnames)
    for letter in songnames.split(" "):
        print(letter[0])

def main():
    passwords, usernames, songnames, songartists = access_data()
    success = login(usernames, passwords)
    if success == True:
        while True:
            random_song, random_artist = song_selection(songnames, songartists)
            split_song(songnames)
            break

main()
Reply
#2
Please post the full text of the error you are getting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Below is the error code for the python code for splitting the random song. I messed around with it a little and i think its to do with splitting a variable but im not sure.

Input your usernameBen
Input your passwordTommy_04
well done you logged in Ben
Livin on a Prayer / 12 / Bon Jovi
Traceback (most recent call last):
File "\documents\Desktop\Python\Course_Work.py", line 56, in <module>
main()
File "\documents\Desktop\Python\Course_Work.py", line 52, in main
split_song(songnames)
File "\documents\Desktop\Python\Course_Work.py", line 43, in split_song
for letter in songnames.split(" "):
AttributeError: 'list' object has no attribute 'split'
>>>
Reply
#4
When you call...

while True:
            random_song, random_artist = song_selection(songnames, songartists)
            split_song(songnames)
            break
You are passing songnames (a list) to split_song. A list does not have "split" as the error mentions. Instead you should pass the random_song that you have chosen previously in song_selection.

And then something like:
def split_song(random_song):
    for word in random_song.split(" "):
        print(word[0])
By the way, your username/password checking process allows the users to guess if any other person has certain password, what is in most scenarios a bad idea.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Song Modifying Program Starting Point BadenJaden 1 1,834 Mar-27-2020, 11:36 AM
Last Post: pyzyx3qwerty
  random selection of song and artist not working Unknown_Relic 2 2,318 Nov-08-2019, 02:06 PM
Last Post: perfringo
  How to play a song .wav or .mp3 with audioop native library IvanSilva 3 6,478 Mar-14-2018, 10:49 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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