【问题标题】:How to fix this code to do with correct answers on a quiz如何修复此代码以处理测验中的正确答案
【发布时间】: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


【解决方案1】:

如果你的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

你的songs.txt 看起来像这样:

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))

【讨论】:

  • 嗨,我将代码放入我所拥有的其余部分 - 并编辑了我所拥有的文件 - 它有效!感谢您的帮助。
猜你喜欢
  • 2020-04-01
  • 1970-01-01
  • 2015-09-06
  • 1970-01-01
  • 2021-04-24
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
相关资源
最近更新 更多