可能是因为读取文件时没有删除文件中的空格、换行符或其他不必要的字符。为了解决这个问题,可以使用strip()函数来去除不必要的字符。
示例代码:
with open('words.txt', 'r') as file:
content = file.read()
words = content.strip().split()
count = 0
for word in words:
if word in game_word_list:
count += 1
print("The number of words in the game is:", count)
在这个例子中,我们首先打开文件,并使用read()函数读取文件的内容。然后,通过strip()函数去除文件中的空格和换行符,并使用split()函数将字符串分割成单独的单词。接下来,我们遍历这些单词并计算它们在游戏中出现的次数。最后,我们使用print()函数输出统计结果。