为什么正确答案同时打印 score = 3 和 score = 1?
Why is the correct answer printing both score = 3 and score = 1?
当你输入正确答案("Bad Guy")时,它同时打印score = 3和score = 1?我如何防止这种情况发生,以便在一次尝试正确后,它显示(并保存)分数 = 3,而在第二次尝试正确后,它显示(并保存)分数为 1?顺便说一句,如果玩家第一次猜对了,他们将获得 3 points.If,他们猜错了,他们会再试一次。如果他们第二次猜对,他们只会获得 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
。您向用户的唯一报告是文字字符串 Score = 3
和 Score = 1
。你需要像
这样的东西
print("you got 3 more points; new score =", score)
当你输入正确答案("Bad Guy")时,它同时打印score = 3和score = 1?我如何防止这种情况发生,以便在一次尝试正确后,它显示(并保存)分数 = 3,而在第二次尝试正确后,它显示(并保存)分数为 1?顺便说一句,如果玩家第一次猜对了,他们将获得 3 points.If,他们猜错了,他们会再试一次。如果他们第二次猜对,他们只会获得 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
。您向用户的唯一报告是文字字符串 Score = 3
和 Score = 1
。你需要像
print("you got 3 more points; new score =", score)