为什么同时打印分数= 3和分数= 1的正确答案?

时间:2019-10-28 23:22:04

标签: python python-3.3

当您输入正确的答案(“坏家伙”)时,它会同时打印分数= 3和分数= 1?如何防止这种情况,所以在一次尝试正确后,它显示(并保存)分数= 3,而在第二次尝试正确后,它显示(并保存)分数为1?顺便说一句,如果玩家第一次猜对了,他们会得到3分;如果弄错了,他们会再试一次。如果他们第二次猜对了,他们只会获得1分。如果他们仍然认为错误,则游戏结束。请说明问题所在,然后尝试解决此问题。

这是我正在使用的代码:

score = 0
print("Round 1:")
with open('songs.txt') as songs_list:
song_name = songs_list.readline()
answer = input("What is the song name? " + song_name)
if answer == "Bad Guy":
    score+=3
    print("Correct! Score = 3")   
else:
    print("Incorrect! Try again.")
    answer = input("What is the song name? " + song_name)
if answer == "Bad Guy":
    score +=1
    print("Correct! Score = 1")
else:
    print("Incorrect! Unlucky, you lost. I hope you enjoyed playing!")
    import sys
    sys.exit()

2 个答案:

答案 0 :(得分:1)

您应该缩进第二组if-else语句。这意味着只有在第一个答案错误的情况下,第二组才会出现。

score = 0
print("Round 1:")
with open('songs.txt') as songs_list:
song_name = songs_list.readline()
answer = input("What is the song name? " + song_name)
if answer == "Bad Guy":
    score+=3
    print("Correct! Score = 3")   
else:
    print("Incorrect! Try again.")
    answer = input("What is the song name? " + song_name)
    if answer == "Bad Guy":
        score +=1
        print("Correct! Score = 1")
    else:
        print("Incorrect! Unlucky, you lost. I hope you enjoyed playing!")
        import sys
        sys.exit()

答案 1 :(得分:0)

您最直接的问题是,您永远不会打印score。您仅向用户报告的是文字字符串Score = 3Score = 1。您将需要类似

的内容
print("you got 3 more points; new score =", score)