Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复此代码以处理测验中的正确答案_Python_Python 3.3 - Fatal编程技术网

Python 如何修复此代码以处理测验中的正确答案

Python 如何修复此代码以处理测验中的正确答案,python,python-3.3,Python,Python 3.3,我正在进行NEA评估,我似乎无法修复这段代码。在本例中,代码提供艺术家姓名BTS和歌曲F的首字母F for Fire。当我输入Fire时,它显示为不正确 不完全确定在这里写什么作为背景。这是我第一次在Python上尝试这个。我试过在这里做些小改动,但没用 file = open("artistname.txt", "r") artist = file.readline() file.close()#opens file, reads it into artist variable and cl

我正在进行NEA评估,我似乎无法修复这段代码。在本例中,代码提供艺术家姓名BTS和歌曲F的首字母F for 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
我希望输出是“正确的:+3点,当我输入火,并给我第二次机会,只有当我键入错误的答案,然后一次游戏,我得到了第二次错误。相反,无论我键入什么,它都会重复错误,直到游戏结束。

如果您的artists.txt如下所示:

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
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
您的songs.txt如下所示:

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
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
然后,您需要做的不仅仅是file.readline。readline只读取文件中的一行。如果您的文件实际上只有一行长,所有艺术家/歌曲都用空格隔开,那么readline可以工作-我不建议您这样构造数据。我至少会把每一位艺术家/每首歌放在一行上,就像我上面展示的那样。如果您只有一个文件,比如.csv逗号分隔值文件,艺术家和歌曲会显式地配对在一起,那就更好了

此代码假设您有两个文件,其中每个艺术家/歌曲位于单独的一行:

with open("artists.txt", "r") as file:
    artists = file.read().splitlines()

with open("songs.txt", "r") as file:
    songs = file.read().splitlines()

score = 0

# The amount of points you can earn for a given attempt
# You earn three points if you guess correctly on your first attempt
# You earn 1 point if you guess correctly on your second attempt
attempt_points = [3, 1]
max_attempts = len(attempt_points)

for artist, song in zip(artists, songs):
    song_initial = song[0]
    print("The artist is \"{}\" and the song's title begins with '{}'.".format(artist, song_initial))

    for current_attempt, points in enumerate(attempt_points, start=1):
        user_input = input("Enter your guess ({} attempt(s) remaining): ".format(max_attempts-current_attempt+1))

        if user_input == song:
            print("Correct: +{} points".format(points))
            score += points
            break
        elif current_attempt < max_attempts:
            print("Incorrect: Try again.")
    else:
        print("Game over.")
        break
    print("Score: {}".format(score))

else:
    print("Congratulations! You've managed to guess all songs correcly!")

print("Your total score is {}.".format(score))

歌曲和艺术家的内容是什么?不是描述,而是实际数据。您的文件很可能有超过1个艺术家/歌曲。如果没有,它们后面可能有换行符,因此您可以比较歌曲与歌曲\n。使用print f'-{song}-'和print f'-{artist}-'来验证它们到底是什么。那就继续读吧。不要评判哈哈。我是K-Pop的书呆子。歌曲内容:``火焰停留花式小姐梦幻般的婴儿偶像我永远年轻不Gogobe自负的Zimzalabim是或是狄俄尼索斯杀死这个爱敲敲敲妖怪Peekaboo Latata``艺术家内容:`` BTS黑粉两次GI dle BigBang BTS CLC两次黑粉CLC Mamamoo Mamamoo红色天鹅绒两次BTS黑粉两次CLC红色天鹅绒Gidle``所有歌曲的名字都与艺术家的名字相匹配。大多数名字之间的每一个空格都是独立的行。嗨,我把代码放进了我所拥有的其余部分,编辑了我所拥有的文件,并且它工作了!谢谢你的帮助。