分级机未正确计算输出
The grader doesn't calculate output correctly
我目前正在研究 edX Python 课程中的问题,目标是创建一个像 "Scrambler" 这样的游戏。我正处于 "playHand" 的步骤,这基本上是与 player/user 的交互,它在每个单词作为输入给出后输出一个分数。
我已经编写了整个过程,并且它在在线编译器(python tutor)中完美运行。但是,当我在课程网站上的 IDE 中输入相同的代码时,它应该对我的答案进行评分并测试它自己的示例,正确的结果仅在第一次测试时出现(分数与预期的分数匹配)。当第二个测试通过时,分数会累加到前一个测试的分数上,因此比需要的要大。
# some of the helper functions are dropped out from this code (but can be provided if needed)
# worldList is the list of words that are valid
single_period=["."]
score=0
def playHand(hand, wordList, n):
while calculateHandlen(hand) > 0:
global score
if n<calculateHandlen(hand):
print("n should be bigger than number of letters in the hand")
break
print("Current Hand: ",end =" ")
displayHand(hand)
word = input("Enter word, or a " + '"." ' + "to indicate that you are finished: ")
if word in single_period:
print("Goodbye! Total score: "+str(score)+" points")
break
else:
if isValidWord(word, hand, wordList)!=True:
print("Ivalid word, please try again.")
print('')
else:
word_score=getWordScore(word, n)
score=score+getWordScore(word, n)
print("'"+str(word)+"'"+" earned "+str(word_score)+" points."+" Total: "+str(score)+" points")
hand=updateHand(hand, word)
if calculateHandlen(hand)==0:
print("Run out of letters. Total score: "+str(score)+" points.")
例如第一个测试是:
Function call: playHand({i: 1, k: 1, l: 1, m: 1})'<edX internal wordList>', 4
我的输出是(正确的):
Current Hand: k i m l
Enter word, or a "." to indicate that you are finished: milk
'milk' earned 90 points. Total: 90 points
Run out of letters. Total score: 90 points.
None
第二个测试是:
Function call: playHand({a: 1, z: 1})'<edX internal wordList>', 2
我的输出是(错误地过度累积):
Current Hand: z a
Enter word, or a "." to indicate that you are finished: zo
Ivalid word, please try again.
Current Hand: z a
Enter word, or a "." to indicate that you are finished: za
'za' earned 72 points. Total: 162 points
Run out of letters. Total score: 162 points.
None
*** ERROR: Failing on scoring the word.
Expected '" za " earned 72 points. Total: 72 points'
Got ''za' earned 72 points. Total: 162 points' ***
因此,正如所见,该测试采用了之前测试的分数 (90) 而不是 "zeroing",将其用作第二次测试的新基础 (90+72=162)等等...
有没有人参加过这门课程或知道如何解决这个问题?
看来他们并不希望您通过手牌积累积分。
我猜 IDE 多次调用 playHand
,您将手牌得分保存在全局得分变量 (global score
) 中,该得分仅设置为 0曾经在你的职能之外。
你可以解决的问题制作:
print("'"+str(word)+"'"+" earned "+str(word_score)+" points."+" Total: "+str(score)+" points")
这个:
print("'"+str(word)+"'"+" earned "+str(word_score)+" points."+" Total: "+str(word_score)+" points")
或在playHand
开头重置为0score
。
我目前正在研究 edX Python 课程中的问题,目标是创建一个像 "Scrambler" 这样的游戏。我正处于 "playHand" 的步骤,这基本上是与 player/user 的交互,它在每个单词作为输入给出后输出一个分数。
我已经编写了整个过程,并且它在在线编译器(python tutor)中完美运行。但是,当我在课程网站上的 IDE 中输入相同的代码时,它应该对我的答案进行评分并测试它自己的示例,正确的结果仅在第一次测试时出现(分数与预期的分数匹配)。当第二个测试通过时,分数会累加到前一个测试的分数上,因此比需要的要大。
# some of the helper functions are dropped out from this code (but can be provided if needed)
# worldList is the list of words that are valid
single_period=["."]
score=0
def playHand(hand, wordList, n):
while calculateHandlen(hand) > 0:
global score
if n<calculateHandlen(hand):
print("n should be bigger than number of letters in the hand")
break
print("Current Hand: ",end =" ")
displayHand(hand)
word = input("Enter word, or a " + '"." ' + "to indicate that you are finished: ")
if word in single_period:
print("Goodbye! Total score: "+str(score)+" points")
break
else:
if isValidWord(word, hand, wordList)!=True:
print("Ivalid word, please try again.")
print('')
else:
word_score=getWordScore(word, n)
score=score+getWordScore(word, n)
print("'"+str(word)+"'"+" earned "+str(word_score)+" points."+" Total: "+str(score)+" points")
hand=updateHand(hand, word)
if calculateHandlen(hand)==0:
print("Run out of letters. Total score: "+str(score)+" points.")
例如第一个测试是:
Function call: playHand({i: 1, k: 1, l: 1, m: 1})'<edX internal wordList>', 4
我的输出是(正确的):
Current Hand: k i m l
Enter word, or a "." to indicate that you are finished: milk
'milk' earned 90 points. Total: 90 points
Run out of letters. Total score: 90 points.
None
第二个测试是:
Function call: playHand({a: 1, z: 1})'<edX internal wordList>', 2
我的输出是(错误地过度累积):
Current Hand: z a
Enter word, or a "." to indicate that you are finished: zo
Ivalid word, please try again.
Current Hand: z a
Enter word, or a "." to indicate that you are finished: za
'za' earned 72 points. Total: 162 points
Run out of letters. Total score: 162 points.
None
*** ERROR: Failing on scoring the word.
Expected '" za " earned 72 points. Total: 72 points'
Got ''za' earned 72 points. Total: 162 points' ***
因此,正如所见,该测试采用了之前测试的分数 (90) 而不是 "zeroing",将其用作第二次测试的新基础 (90+72=162)等等...
有没有人参加过这门课程或知道如何解决这个问题?
看来他们并不希望您通过手牌积累积分。
我猜 IDE 多次调用 playHand
,您将手牌得分保存在全局得分变量 (global score
) 中,该得分仅设置为 0曾经在你的职能之外。
你可以解决的问题制作:
print("'"+str(word)+"'"+" earned "+str(word_score)+" points."+" Total: "+str(score)+" points")
这个:
print("'"+str(word)+"'"+" earned "+str(word_score)+" points."+" Total: "+str(word_score)+" points")
或在playHand
开头重置为0score
。