Python音乐猜谜游戏答案验证失败问题求助
音乐猜谜游戏判定错误问题排查与修复
问题描述
我用Python开发了一款音乐猜谜游戏,核心逻辑是从Songs.txt读取歌曲列表,随机选取1首歌曲,提取歌名每个单词的首字母并显示对应歌手,用户拥有两次猜歌名的机会并按规则计分。但无论输入正确歌名(含大小写、首字母大写等形式),程序始终判定答案错误。
原程序代码
# 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("Your 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()
文件内容
Songs.txt:
Let It Happen New Gold Shotgun Metamodernity Bad Guy Blank Space Bohemian Rhapsody
Artists.txt:
Tame Impala Gorillaz George Ezra Vansire Billie Eillish Taylor Swift Queen
错误运行示例
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.
问题原因
- 换行符残留:
linecache.getline()读取的每行内容末尾会包含换行符\n,比如读取到的song实际值是"NEW GOLD\n",而用户输入的内容不包含这个换行符,直接比对必然不相等。 - 大小写不兼容:原代码仅将读取的歌名转为大写,但用户输入可能是混合大小写(如
New Gold),直接比对会因大小写差异判定错误。
修复方案
- 对读取的歌曲名使用
.strip()去除首尾空白字符(包括换行符),再转为大写。 - 将用户的输入也转为大写后再进行比对,忽略大小写差异。
修复后的完整代码
# MUSIC QUIZ GAME import random import sys import linecache userscore = 0 random_picker = random.randint(1,7) # ----- Accessing the Songs database ----- # # 读取歌曲名并去除首尾空白,转大写 song = linecache.getline(r"Songs.txt", random_picker).strip().upper() song_prompt = song.split() # ----- Accessing the Artists database ----- # # 读取歌手名并去除首尾空白,避免多余换行 artist = linecache.getline(r"Artists.txt", random_picker).strip() # -----Giving the user the prompt ----- # print("Your prompt is: ") for word in song_prompt: print(word[0]) print(f"by {artist}") guess1 = input("Enter your song name guess:\n").strip().upper() if guess1 == song: print("Well done!") userscore += 3 print(f"Your score is {userscore}.") else: print("Wrong guess.") guess2 = input("What is your second guess for the song name?\n").strip().upper() if guess2 == song: print("Well done.") userscore += 1 print(f"Your score is {userscore}.") else: print("You have gotten both guesses wrong.") sys.exit()
内容的提问来源于stack exchange,提问作者Boxo Games
相关产品推荐
相关产品推荐

