-2

I am creating a music quiz. Below is my CSV file. I would like to select a song and artist at random from the file - displaying the artist name but only the FIRST LETTER of each word in the song name to be displayed.

I was wondering if any of you knew how I could do this on Python. The file name is called playlist.csv

I have tried several different methods such as the one below, but it does not work.

with open('playlist.csv') as mycsv:
    print(mycsv.readline()[0]) 

I've already imported CSV file and stored artists and songs in CSV file.

Songs                                     Artists

Maroon 5 Girls Like You                   Andrea Garcia 
I Like It                                 Cardi B   
God's Plan                                Drake 
No Tears Left To Cry                      Ariana Grande 
Psycho                                    Post Malone   
The Middle                                Maren Morris  
Better Now                                Post Malone   
In My Feelings                            Drake 
It's Coming Home                          The Lightning Seeds   
One Kiss                                  Dua Lipa  

A random song and artist is chosen.

For example:

M, Andrea Garcia

G, Drake

B, Post Malone

O, Dua Lipa.

At the moment, I am also trying to create an if statement where, if the user's guess for the randomly generated song is correct, they get a message saying "Correct answer". This is what I've attempted at the moment.

    userName = str
    password = str
    userName = raw_input("What is your name?")
    password = raw_input("Please enter the correct password.")

if userName == "John" or "John" or password!= "musicquiz":
    print("Sorry, the username is taken and/or the password is incorrect.")
    quit()

if userName!= "John" and password == "musicquiz":
    print("Hello", userName, "welcome to the game!")

import random
with open('playlist.csv') as mycsv:
    f=mycsv.readlines()[1:]
    random_line=random.choice(f).split(',')
    print(random_line[0][0],",",random_line[3])

userGuess = str
userScore = int
userScore = 0

userGuess = raw_input("What is the correct name of this song?")

if userGuess == true:
    print("Well done, that was the correct answer. You have scored three points")
    userScore + 3

else:
    print("Sorry, your guess was incorrect. You have one more chance to guess it right")
    raw_input("What is the correct name of this song?")

if userGuess != true:
    print("Sorry, game over.")
    quit()

if userGuess == true:
    print("Well done, that was the correct answer. You scored one point.")
    userScore + 1
harik2014
  • 11
  • 1
  • 1
  • 4
  • Maybe you could this question could inspire you: https://stackoverflow.com/questions/53884270/how-do-you-check-a-randomly-generated-value-from-an-array-against-user-input-in/ – EvensF Dec 31 '18 at 20:41
  • thank you for the reply. Do you know how I can use an if statement where, if the user's guess for the song is correct, I can use the print() function to tell the user that their guess has been correct? – harik2014 Jan 01 '19 at 17:16
  • Well, what did you try? – EvensF Jan 01 '19 at 17:37
  • Modify your question description to include your code attempt because right now I'm not able to see what is not working with your code. – EvensF Jan 01 '19 at 22:12
  • Hello, apologies. I have now modified my question. – harik2014 Jan 02 '19 at 09:42
  • The code you have provided is not complete. Where does `userGuess` or `random_line` come from? Provide something I can copy and run on my computer. – EvensF Jan 02 '19 at 16:08
  • Again, I'm very sorry. I have again modified my question with the entire code - you should be able to run the code now. – harik2014 Jan 02 '19 at 17:48
  • I'm sorry but your code doesn't work. There's too many errors. (For example, there is no `quit()` function) You have to go back to the basics and learn how to program in Python. I would recommend that you read for example the [official tutorial](https://docs.python.org/3/tutorial/index.html) – EvensF Jan 03 '19 at 14:44
  • The quit() function works on my Python version and I'm using version 2.7.1 – harik2014 Jan 03 '19 at 18:06

2 Answers2

0
import random
with open('playlist.csv') as mycsv:
    f=mycsv.readlines()[1:] #all except header
    random_line=random.choice(f).split(',')
    print(random_line[0][0],",",random_line[1])

Output

M , Andrea Garcia

playlist.csv

Songs,Artists
Maroon 5 Girls Like You,Andrea Garcia
I Like It,Cardi B
God's Plan,Drake
No Tears Left To Cry,Ariana Grande
Psycho,Post Malone
The Middle,Maren Morris
Better Now,Post Malone
In My Feelings,Drake
It's Coming Home,The Lightning Seeds
One Kiss,Dua Lipa 
Bitto
  • 7,937
  • 1
  • 16
  • 38
0

I waited a few weeks to give you my answer to prevent you from not completing yourself what looks a lot like a homework

So first of all, your input file:

playlist.csv

Songs,Artists
Maroon 5 Girls Like You,Andrea Garcia 
I Like It,Cardi B
God's Plan,Drake 
No Tears Left To Cry,Ariana Grande 
Psycho,Post Malone
The Middle,Maren Morris
Better Now,Post Malone
In My Feelings,Drake 
It's Coming Home,The Lightning Seeds
One Kiss,Dua Lipa

And the code:

import csv
import random

with open('playlist.csv', 'rb') as input_file:
    csv_source = csv.DictReader(input_file)   
    all_songs_and_artists = list(csv_source)

song_and_artist_chosen = random.choice(all_songs_and_artists)

song_words = song_and_artist_chosen["Songs"].split()
song_initials = "".join(item[0] for item in song_words)
initials_uppercase = song_initials.upper()

print "Welcome to the music quiz"
print "-------------------------"
print "\n"
print "Here's an artist: " + song_and_artist_chosen["Artists"]
print "and the initials of the song we are searching are: " + initials_uppercase
user_guess = raw_input("What is the name of this song? ")

if song_and_artist_chosen["Songs"].upper() == user_guess.upper():
    print "Well done, that was the correct answer."
else:
    print "Sorry, your guess was incorrect. You have one more chance to guess it right"
    user_guess = raw_input("What is the name of this song? ")
    if song_and_artist_chosen["Songs"].upper() == user_guess.upper():
        print "Well done, that was the correct answer."
    else:
        print "Sorry, game over."
EvensF
  • 1,479
  • 1
  • 10
  • 17