我正在为我的OCR GCSE编程项目进行音乐测验。 python程序的目的是从数组中生成随机选择的歌曲,显示歌曲的首字母并显示艺术家,然后允许用户猜测歌曲的名称。歌曲数组和艺术家数组存储在单独的外部记事本文件中,并正确加载,显示歌曲和艺术家的缩写。我的问题是,即使用户正确猜出了歌曲名称,程序也会显示它不正确,并且与用户输入的歌曲名称不匹配。
我尝试显示歌曲名称以确保正确猜出歌曲名称,并且还尝试复制歌曲名称并将其复制到用户输入中
import random
songlistfilecontents = open("songlist.txt", "r")
songlist = songlistfilecontents.readlines()
artistlistfilecontents = open("artistlist.txt", "r")
artistlist = artistlistfilecontents.readlines()
randomnumber = random.randint(0,11)
randomsong = songlist[randomnumber]
randomartist = artistlist [randomnumber]
initialsofsong = "".join(item[0].upper() for item in randomsong.split())
counter = 0
print("The songs' initials are " ,initialsofsong, " and the name of the
artist is " ,randomartist)
print (randomsong)
songnameguess = input("Guess the name of the song!")
counter = counter + 1
while songnameguess != randomsong:
songnameguess = input("Nope! Try again!")
counter = counter + 1
if counter >=3 and songnameguess != randomsong:
print ("Sorry, you've had two chances. Come back soon!")
elif songnameguess == randomsong:
print ("Well done!")
我希望程序显示“做得好!”如果用户对歌曲的猜错不超过3次并正确猜对了答案。但是,程序从不显示此内容,而是显示Nope!再试一次并提示输入Songnameguess,直到用户猜测了3次(错误或正确),然后打印对不起,您有两次机会。很快回来!
答案 0 :(得分:0)
就像@Barmar在阅读文本文件时在注释中说的那样,您必须考虑以下事实:您将在每一行的末尾获得换行符。但是您的代码中还有另一个错误:在您的while
循环中,您永远不会检查用户给出的答案是否超过您想要授予的答案。因此,用户将一直停留在该循环中,直到他给出正确的答案为止。
因此,只需进行很少的修改,它就会像:
解决方案1
import random
songlistfilecontents = open("songlist.txt", "r")
songlist = songlistfilecontents.readlines()
artistlistfilecontents = open("artistlist.txt", "r")
artistlist = artistlistfilecontents.readlines()
randomnumber = random.randint(0,11)
randomsong = songlist[randomnumber]
randomsong = randomsong.rstrip("\n")
randomartist = artistlist [randomnumber]
initialsofsong = "".join(item[0].upper() for item in randomsong.split())
counter = 0
print("The songs' initials are " ,initialsofsong, " and the name of the artist is " ,randomartist)
print (randomsong)
songnameguess = input("Guess the name of the song!")
counter = counter + 1
while counter < 3 and songnameguess != randomsong :
songnameguess = input("Nope! Try again!")
counter = counter + 1
if counter >=3 and songnameguess != randomsong:
print ("Sorry, you've had two chances. Come back soon!")
elif songnameguess == randomsong:
print ("Well done!")
答案 1 :(得分:0)
但是我们可以走得更远。
解决方案2
import random
with open("songlist.txt", "r") as songlistfilecontents:
songlist = songlistfilecontents.readlines()
with open("artistlist.txt", "r") as artistlistfilecontents:
artistlist = artistlistfilecontents.readlines()
randomnumber = random.randint(0,11)
randomsong = songlist[randomnumber]
randomsong = randomsong.rstrip("\n")
randomartist = artistlist [randomnumber]
initialsofsong = "".join(item[0].upper() for item in randomsong.split())
print("The songs' initials are", initialsofsong, "and the name of the artist is", randomartist)
print (randomsong)
# First try
songnameguess = input("Guess the name of the song! ")
nb_tries_left = 2
answer_not_found = (songnameguess != randomsong)
while nb_tries_left > 0 and answer_not_found:
songnameguess = input("Nope! Try again! ")
nb_tries_left -= 1
answer_not_found = (songnameguess != randomsong)
if answer_not_found:
print ("Sorry, you've had two chances. Come back soon!")
else:
print ("Well done!")
nb_tries_left
来记录停止之前剩余的尝试次数。我没有将值累加起来,而是先将该值设置为零,然后递减为零。答案 2 :(得分:0)
我们可以走得更远:
解决方案3
import random
with open("songlist.txt", "r") as songs_file:
with open("artistlist.txt", "r") as artists_file:
songs_and_artists = [(song.rstrip('\n'), artist.rstrip('\n'))
for (song, artist) in zip(songs_file, artists_file)]
random_song, random_artist = random.choice(songs_and_artists)
songs_intials = "".join(item[0].upper() for item in random_song.split())
print("The songs' initials are", songs_intials, "and the name of the artist is", random_artist)
print(random_song)
nb_tries_left = 3
guess = input("Guess the name of the song! ")
nb_tries_left -= 1
finished = False
while not finished:
answer_found = (guess == random_song)
if not answer_found:
guess = input("Nope! Try again! ")
nb_tries_left -= 1
finished = (answer_found or nb_tries_left <= 0)
if answer_found:
print ("Well done!")
else:
print ("Sorry, you've had two chances. Come back soon!")
zip()
,我创建了一个包含歌曲和艺术家组合的元组列表。 random.choice()
随机选择一首歌曲和一位歌手