0

I'm doing a music quiz for my OCR GCSE programming project. The aim of the python program is to generate a randomly chosen song from an array, display the initials of the song and also show the artist, then allow the user to guess the name of the song. The song array and the artist array are stored in separate external notepad files, and load in correctly, displaying the initials of the song and the artist. My problem is that, even if the user guesses the song name correctly, the program displays that it is incorrect and does not match the correct song name to the user input.

I have tried displaying the song name to make sure I am guessing the song name correctly, and have also tried copying the song name and copying it into the user input

import random
songlistfilecontents = open("songlist.txt", "r")
songlist = songlistfilecontents.readlines()
artistlistfilecontents = open("artistlist.txt", "r")
artistlist = artistlistfilecontents.readlines()
randomnumber = random.randint(0,11)
randomsong = songlist[randomnumber]
randomartist = artistlist [randomnumber]
initialsofsong = "".join(item[0].upper() for item in randomsong.split())

counter = 0
print("The songs' initials are " ,initialsofsong, " and the name of the 
artist is " ,randomartist)
print (randomsong)
songnameguess = input("Guess the name of the song!")
counter = counter + 1
while songnameguess != randomsong:
    songnameguess = input("Nope! Try again!")
    counter = counter + 1
if counter >=3 and songnameguess != randomsong:
    print ("Sorry, you've had two chances. Come back soon!")
elif songnameguess == randomsong:
    print ("Well done!")

I expect the program to display "Well done!" if the user has not guessed the song incorrectly more than 3 times and guesses the answer correctly. However, the program never shows this and instead displays Nope! Try again and prompts for input for songnameguess until the user has guessed (incorrectly or correctly) three times, then prints Sorry, you've had two chances. Come back soon!

roganjosh
  • 12,594
  • 4
  • 29
  • 46
klassnyy
  • 23
  • 1
  • 7
  • 2
    The "disqualified" line is not relevant at all to how Stack Overflow works. But anyway `if counter >=3 and songnameguess != randomsong:` is not inside the `while` loop, so they could have 100 guesses before they find out they were wrong. – roganjosh Dec 21 '18 at 11:49
  • I have tried your advice, but it doesn't provide a solution to the problem I mentioned. The guess provided by the user is still always incorrect. – klassnyy Dec 21 '18 at 11:55
  • The lines returned by `readlines()` have newlines at the end. – Barmar Dec 21 '18 at 12:13
  • Related: [reading a file without newlines](https://stackoverflow.com/questions/12330522/reading-a-file-without-newlines/12330535#12330535) – Barmar Dec 21 '18 at 12:14

3 Answers3

0

Like @Barmar said in the comments when you read a text file, you have to take into account the fact that you are going to get the newline character at the end of each line. But there's another bug in your code: in your while loop you never check to see if the user has given more answers than you wanted to grant him. So the user will be stuck in that loop until he gives the right anwser.

So with minimal modifications it would look like:

Solution 1

import random
songlistfilecontents = open("songlist.txt", "r")
songlist = songlistfilecontents.readlines()
artistlistfilecontents = open("artistlist.txt", "r")
artistlist = artistlistfilecontents.readlines()
randomnumber = random.randint(0,11)
randomsong = songlist[randomnumber]
randomsong = randomsong.rstrip("\n")
randomartist = artistlist [randomnumber]
initialsofsong = "".join(item[0].upper() for item in randomsong.split())

counter = 0
print("The songs' initials are " ,initialsofsong, " and the name of the artist is " ,randomartist)
print (randomsong)
songnameguess = input("Guess the name of the song!")
counter = counter + 1
while counter < 3 and songnameguess != randomsong :
    songnameguess = input("Nope! Try again!")
    counter = counter + 1

if counter >=3 and songnameguess != randomsong:
    print ("Sorry, you've had two chances. Come back soon!")
elif songnameguess == randomsong:
    print ("Well done!")
EvensF
  • 1,479
  • 1
  • 10
  • 17
0

But we go can go further.

Solution 2

import random

with open("songlist.txt", "r") as songlistfilecontents:
    songlist = songlistfilecontents.readlines()

with open("artistlist.txt", "r") as artistlistfilecontents:
    artistlist = artistlistfilecontents.readlines()

randomnumber = random.randint(0,11)
randomsong = songlist[randomnumber]
randomsong = randomsong.rstrip("\n")
randomartist = artistlist [randomnumber]
initialsofsong = "".join(item[0].upper() for item in randomsong.split())


print("The songs' initials are", initialsofsong, "and the name of the artist is", randomartist)
print (randomsong)
# First try
songnameguess = input("Guess the name of the song! ")
nb_tries_left = 2
answer_not_found = (songnameguess != randomsong)
while nb_tries_left > 0 and answer_not_found:
    songnameguess = input("Nope! Try again! ")
    nb_tries_left -= 1
    answer_not_found = (songnameguess != randomsong)

if answer_not_found:
    print ("Sorry, you've had two chances. Come back soon!")
else:
    print ("Well done!")
  • I've used context managers to open and read files
  • I've used nb_tries_left to keep note of the number of tries left before stopping. Instead of counting up to a value, I set that value first and decrement to zero.
EvensF
  • 1,479
  • 1
  • 10
  • 17
0

We can go even further:

Solution3

import random

with open("songlist.txt", "r") as songs_file:
    with open("artistlist.txt", "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)
songs_intials = "".join(item[0].upper() for item in random_song.split())


print("The songs' initials are", songs_intials, "and the name of the artist is", random_artist)
print(random_song)

nb_tries_left = 3
guess = input("Guess the name of the song! ")
nb_tries_left -= 1

finished = False
while not finished:
    answer_found = (guess == random_song)
    if not answer_found:
        guess = input("Nope! Try again! ")
        nb_tries_left -= 1

    finished = (answer_found or nb_tries_left <= 0) 

if answer_found:
    print ("Well done!")
else:
    print ("Sorry, you've had two chances. Come back soon!")
  • I use the fact that file objects are iterable. So using zip(), I create a list of tuples containing songs and artists combined.
  • I use random.choice() to pick a random song and artist
EvensF
  • 1,479
  • 1
  • 10
  • 17