-4

I'm working on my NEA assessment and I can't seem to fix this bit of code here. The code provides the artist name (in this case BTS) and the first initial of a song (F for Fire). When I type Fire, it comes up as incorrect.

Not entirely sure what to write for background here. This is my first time trying this on Python. I've tried minor alterations here but to no avail.

file =  open("artistname.txt", "r")
artist = file.readline()
file.close()#opens file, reads it into artist variable and closes

file = open("songName.txt", "r")
song = file.readline()
file.close()#opens file, reads it into song variable and closes

score = 0
for round in range(len(artist)):
    print(artist)
    songInitial = song[:1]
    print(songInitial)#prints first initial
    if input() == song:
        print("Correct: +3 points")
        score = score + 3#adds 3 to score
        print("Score = ", score)#prints score
    else:
        print("Incorrect, try again")
        if input() == song:#allows second chance
            print("Correct: +1 point")
            score = score + 1#adds 1 to score
            print("Score = ", score)#prints score
        else:
            print("Incorrect, game over.")
            print("Score = ", score)#prints score
            break#ends code

I expect the output to be 'Correct: +3 points" when I type Fire, and to give me a second chance only when I type the incorrect answer, and then a game over once I get it wrong a second time. Instead, it repeats incorrect until the game ends no matter what I type.

  • What are the contents of `song` & `artist`? Not a description, but the actual data. – Scott Hunter Sep 16 '19 at 12:42
  • most probably your files have more then 1 artist/song if not, they might have newlines after them so you compare "song" vs "song\n" . use `print( f'--{song}--')` and `print( f'--{artist}--')` to verify what exactly is in them. Then readup on ".strip()" – Patrick Artner Sep 16 '19 at 12:42
  • Don't judge haha. I'm a K-Pop nerd. Contents of song: ``` Fire Stay Fancy Senorita Fantastic-Baby Idol Me TT Forever-Young No Gogobebe Egotistic Zimzalabim Yes-or-Yes Dionysus Kill-This-ove Knock-Knock Hobgoblin Peekaboo Latata ``` Contents of artist: ``` BTS BlackPink Twice (G)I-dle BigBang BTS CLC Twice Blackpink CLC Mamamoo Mamamoo Red-Velvet Twice BTS BlackPink Twice CLC Red-Velvet (G)I-dle ``` All the song names match lines to the artist names. Each space between (most) of the names are the separate lines. – Blueless Sep 16 '19 at 12:56

1 Answers1

0

If your artists.txt looks like this:

BTS
BlackPink
Twice
(G)I-dle
BigBang
BTS
CLC
Twice
Blackpink
CLC
Mamamoo
Mamamoo
Red-Velvet
Twice
BTS
BlackPink
Twice
CLC
Red-Velvet
(G)I-dle

And your songs.txt looks like this:

Fire
Stay
Fancy
Senorita
Fantastic-Baby
Idol
Me
TT
Forever-Young
No
Gogobebe
Egotistic
Zimzalabim
Yes-or-Yes
Dionysus
Kill-This-ove
Knock-Knock
Hobgoblin
Peekaboo
Latata

Then you'll need to do more than just file.readline(). readline reads just a single line from the file. If your files really are just one line long (where all artists / songs are separated by spaces), then readline could work - I wouldn't recommend you structure your data like this though. I would at least put each artist / song on a separate line, like I've shown above. Even better would be if you had just one file, like a .csv (comma separated values) file, where artists and songs are explicitly paired together.

This code assumes you have two files, where every artist / song is on a separate line:

with open("artists.txt", "r") as file:
    artists = file.read().splitlines()

with open("songs.txt", "r") as file:
    songs = file.read().splitlines()

score = 0

# The amount of points you can earn for a given attempt
# You earn three points if you guess correctly on your first attempt
# You earn 1 point if you guess correctly on your second attempt
attempt_points = [3, 1]
max_attempts = len(attempt_points)

for artist, song in zip(artists, songs):
    song_initial = song[0]
    print("The artist is \"{}\" and the song's title begins with '{}'.".format(artist, song_initial))

    for current_attempt, points in enumerate(attempt_points, start=1):
        user_input = input("Enter your guess ({} attempt(s) remaining): ".format(max_attempts-current_attempt+1))

        if user_input == song:
            print("Correct: +{} points".format(points))
            score += points
            break
        elif current_attempt < max_attempts:
            print("Incorrect: Try again.")
    else:
        print("Game over.")
        break
    print("Score: {}".format(score))

else:
    print("Congratulations! You've managed to guess all songs correcly!")

print("Your total score is {}.".format(score))
Paul M.
  • 7,663
  • 2
  • 6
  • 10
  • Hi, I put the code into the rest of what I had - and edited the file I had - and it works! Thank you for your help. – Blueless Sep 23 '19 at 12:45