My music quiz game takes a list of songs from an external file named 'Songs.txt' and takes the first letter of each word in one song (out of 7, picked randomly) and then displays the artist of that song. The user then has two guesses, and the appropriate points are then awarded:
# MUSIC QUIZ GAME
import random
# Import the 'random' function in order to select a random number and get a random song.
import time
# Importing the ability to 'pause' to give the program a more professional feel.
import sys
# Import the ability to close the program.
import linecache
# Import the ability to get a specific line from a file.
userscore = 0
# The userscore is automatically zero, because they have just started playing.
random_picker = random.randint(1,7)
# Using the random operator, we pick a random number from 1 to 5, because there are 5 songs. We will use this to select the same random line from two files.
# ----- Accessing the Songs database ----- #
# Open the file.
file = open("Songs.txt", "r")
# Get the line from file. The line number is determined by the 'random_picker' used earlier.
song = linecache.getline(r"Songs.txt", random_picker).upper()
# Split the line into individual words.
song_prompt = song.split()
# ----- Accessing the Artists database ----- #
file = open("Artists.txt", "r")
artist = linecache.getline(r"Artists.txt", random_picker)
# -----Giving the user the prompt ----- #
print("\nYour prompt is: ")
# Get only the first letter of each word in the prompt, then output them.
for word in song_prompt:
print(word[0])
print("by " artist)
guess1 = input("Enter your song name guess:\n")
if guess1 == song:
print("Well done!")
userscore = userscore 3
print("Your score is " str(userscore) ".")
elif guess1 != song:
print("Wrong guess.")
guess2 = input("What is your second guess for the song name?\n")
if guess2 == song:
print("Well done.")
userscore = userscore 1
print("Your score is " str(userscore) ".")
else:
print("You have gotten both guesses wrong.")
sys.exit()
However, on both guesses, with the right song, uppercase, lowercase, capitilised titles, etc, the software always deems the answer wrong. Is it to do with the way the file is read? I'm not quite sure. If anyone could help me with this issue, that would be much appreciated.
The 'Songs.txt' file contains this list:
Let It Happen
New Gold
Shotgun
Metamodernity
Bad Guy
Blank Space
Bohemian Rhapsody
The 'Artists.txt' file contains this list:
Tame Impala
Gorillaz
George Ezra
Vansire
Billie Eillish
Taylor Swift
Queen
For example, the quiz says:
Your prompt is:
N
G
by Gorillaz
Enter your song name guess:
New Gold
Wrong guess.
What is your second guess for the song name?
NEW GOLD
You have gotten both guesses wrong.
I am expecting to get the correct answer.
CodePudding user response:
The issue is happening because the song variable is being assigned the value of the selected song from the "Songs.txt" file, but the guess1 and guess2 inputs are being compared against it as is, and in this case, it's case sensitive, so the comparison will only be true if the input is an exact match, including capitalization and whitespaces.
One solution to this is to convert both the song and the user's input to lowercase, using the str.lower() method, before doing the comparison. This way, the input can match the song even if the capitalization or whitespaces are different.
song = linecache.getline(r"Songs.txt", random_picker).upper().strip()
# ...
guess1 = input("Enter your song name guess:\n").lower()
if guess1 == song.lower():
print("Well done!")
userscore = userscore 3
print("Your score is " str(userscore) ".")
# ...
guess2 = input("What is your second guess for the song name?\n").lower()
if guess2 ==song.lower():
#...
song = linecache.getline(r"Songs.txt", random_picker).upper().strip()
You can see that .lower() method is added to both inputs and song. Also the .strip() method is added to the song variable, to remove the leading and trailing whitespaces and newlines.
CodePudding user response:
I think you might also have an issue with the way you read the .txt files.
When you run the following line: song = linecache.getline(r"Songs.txt", random_picker).upper()
The value assigned to song
will be something like 'LET IT HAPPEN\n'
In order to avoid this you can use the .rstrip() method:
song = linecache.getline(r"Songs.txt", random_picker).upper().rstrip('\n')