dimanche 20 janvier 2019

How to write if statement using CSV files in Pyscripter

I am currently creating a music quiz. The aim is, the user is given two chances to guess the name of the song correctly. Also:

  • If the user guesses the answer correctly the first time, they score three points.
  • If the user guesses the answer correctly is the second time, they score 1 point.
  • If they guess is incorrectly both times, they get 0 points.

I cannot work out how to create an if statement that works using my CSV files in PyScripter, which is shown at the bottom of this question..

This game then needs to repeat, but I cannot work out which loop to use for this to work.

I have copied the code which I have tried previously in the if statement.

userScore = 0
correctAnswer = (guess or guess2 == random_song)
wrongAnswer = (guess or guess2 !=random_song)



if guess == correctAnswer:
    print("Well done. You scored three point!")
    userScore =+ 3

else:
    guess2 = input("Nope. Try again.")
    nb_tries_left -= 1


if guess2 == correctAnswer:
    print ("Well done! You scored one point")
    userScore =+ 1

else:
    print("Sorry, that is again incorrect. Please come again soon")

My entire code:

  userName = input(str("What is your preferred name?"))

import random

with open("songs.csv", "r") as songs_file:
    with open("artists.csv", "r") as artists_file:
        songs_and_artists = [(song.rstrip('\n'), artist.rstrip('\n'))
                             for (song, artist) in zip(songs_file, artists_file)]

random_song, random_artist = random.choice(songs_and_artists)
songLetter = "".join(item[0].upper() for item in random_song.split())

print("The song's first letter is", songLetter, "and the name of the artist is", random_artist)

nb_tries_left = 3
guess = input("Guess the name of the song! ")
nb_tries_left -= 1
guess = int
guess2 = ""
userScore = 0
gameOver = int

correctAnswer = (guess or guess2 == random_song)
wrongAnswer = (guess or guess2 !=random_song)


if guess == correctAnswer:
    print("Well done. You scored three point!")
    userScore =+ 3

else:
    guess2 = input("Nope. Try again.")
    nb_tries_left -= 1


if guess2 == correctAnswer:
    print ("Well done! You scored one point")
    userScore =+ 1

else:
    print("Sorry, that is again incorrect. Please come again soon")

import csv

with open('highscores.csv', 'a', newline='') as csvfile:
    file_writer = csv.writer(csvfile, delimiter=',')
    file_writer.writerow([userName, userScore])

print("The scores are...")
print("------------------------")
with open('highscores.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',', quotechar=',')
    positions = []
    scores = []
    count = 0
    for row in readCSV:
        if row != []:
            currentPos = row[0]
            currentScore = row[1]
            positions.append(currentPos)
            scores.append(currentScore)

    intScores = list(scores)
    count = 0
    while count < 5:
        current = intScores.index(max(intScores))
        print(count+1,"-", positions[current],"'s score is: ", intScores[current])
        del intScores[current]
        del positions[current]
        count +=1

I have also created two separate CSV files - one called songs.csv and artists.csv:

songs.csv

 Maroon 5 Girls Like You
I Like It
God's Plan
No Tears Left To Cry
Psycho
The Middle
Better Now
In My Feelings
It's Coming Home
One Kiss

artists.csv

    Andrea Garcia
Cardi B
Drake
Ariana Grande
Post Malone
Maren Morris
Post Malone
Drake
The Lightning Seeds
Dua Lipa

Aucun commentaire:

Enregistrer un commentaire