【发布时间】:2019-09-16 12:39:47
【问题描述】:
我正在进行我的 NEA 评估,但我似乎无法在此处修复这段代码。该代码提供了艺术家姓名(在本例中为 BTS)和歌曲的首字母(F 代表 Fire)。当我输入 Fire 时,它显示不正确。
不完全确定在这里写什么作为背景。这是我第一次在 Python 上尝试这个。我在这里尝试了一些小改动,但无济于事。
file = open("artistname.txt", "r")
artist = file.readline()
file.close()#opens file, reads it into artist variable and closes
file = open("songName.txt", "r")
song = file.readline()
file.close()#opens file, reads it into song variable and closes
score = 0
for round in range(len(artist)):
print(artist)
songInitial = song[:1]
print(songInitial)#prints first initial
if input() == song:
print("Correct: +3 points")
score = score + 3#adds 3 to score
print("Score = ", score)#prints score
else:
print("Incorrect, try again")
if input() == song:#allows second chance
print("Correct: +1 point")
score = score + 1#adds 1 to score
print("Score = ", score)#prints score
else:
print("Incorrect, game over.")
print("Score = ", score)#prints score
break#ends code
我希望当我输入 Fire 时输出为“正确:+3 分”,并且只有在我输入错误答案时才给我第二次机会,然后在我第二次输入错误时结束游戏。相反,无论我输入什么,它都会重复错误,直到游戏结束。
【问题讨论】:
-
song&artist的内容是什么?不是描述,而是实际数据。 -
很可能你的文件有超过 1 个艺术家/歌曲,如果没有,它们后面可能有换行符,所以你比较 "song" 和 "song\n" 。使用
print( f'--{song}--')和print( f'--{artist}--')来验证它们到底是什么。然后阅读“.strip()” -
不要评判哈哈。我是一个 K-Pop 书呆子。歌曲内容:```Fire Stay Fancy Senorita Fantastic-Baby Idol Me TT Forever-Young No Gogobebe Egotistic Zimzalabim Yes-or-Yes Dionysus Kill-This-ove Knock-Knock Hobgoblin Peekaboo Latata ``` 艺术家内容:`` ` BTS BlackPink Twice (G)I-dle BigBang BTS CLC Twice Blackpink CLC Mamamoo Mamamoo Red-Velvet Twice BTS BlackPink Twice CLC Red-Velvet (G)I-dle ``` 所有歌曲名称都与艺术家姓名一致。 (大多数)名称之间的每个空格都是单独的行。
标签: python python-3.3