splitting a random song - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: splitting a random song (/thread-22473.html) |
splitting a random song - Unknown_Relic - Nov-14-2019 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() RE: splitting a random song - ichabod801 - Nov-14-2019 Please post the full text of the error you are getting. RE: splitting a random song error message - Unknown_Relic - Nov-15-2019 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' >>> RE: splitting a random song - baquerik - Nov-15-2019 When you call... while True: random_song, random_artist = song_selection(songnames, songartists) split_song(songnames) breakYou 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. |