我有一个程序,随机选择和打印歌曲名称和艺术家从外部文本文件。 我已经将行拆分为a,b格式,但一旦我回答了问题,程序将无法识别变量
file = open("Songz.txt", "r")
lines = file.readlines()
random_lines = random.choice(open("Songz.txt").readlines())
Song = random_lines
a,b=(Song.split(","))
print(a)
print(b)
Answer1 = input().upper()
if Answer1 == b:
print(" Correct ")
Quiz_Score = Quiz_Score + 3
else:
print("incorrect")
一旦代码到达
if Answer1 == b:
第节,代码似乎忘记了变量(b)的值。知道为什么吗
问题似乎出在您从文件中读取的数据上。 实际上,使用
readlines
方法得到的列表最后会有额外的\n
符号所以您需要使用
strip()
来清理它们:相关问题 更多 >
编程相关推荐