Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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 为什么正确答案同时打印分数=3和分数=1?_Python_Python 3.3 - Fatal编程技术网

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

Python 为什么正确答案同时打印分数=3和分数=1?,python,python-3.3,Python,Python 3.3,当您输入正确答案(“坏人”)时,它会同时打印分数=3和分数=1?如何防止这种情况发生?在一次尝试正确后,它会显示(并保存)分数=3,在第二次尝试正确后,它会将分数显示(并保存)为1?顺便说一句,如果玩家第一次猜对了,他们会得到3分。如果他们猜错了,他们会再试一次。如果他们第二次猜对了,他们只会得到1分。如果他们仍然错了,游戏就结束了。请解释问题所在并尝试解决此问题 以下是我用于此目的的代码: score = 0 print("Round 1:") with open('songs.txt') a

当您输入正确答案(“坏人”)时,它会同时打印分数=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()

您应该缩进第二组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()

最直接的问题是,您从未打印
分数
。您只向用户报告文本字符串
Score=3
Score=1
。你需要像这样的东西

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