分级机无法正确计算输出

时间:2019-09-24 07:39:39

标签: python edx

我目前正在edX Python课程中研究该问题,目标是创建一个类似“ Scrambler”的游戏。我正在“ playHand”的步骤上,该步骤基本上是与玩家/用户的交互,在给出每个单词作为输入后输出分数。

我已经对整个过程进行了编码,并且可以在在线编译器(python导师)中完美运行。但是,当我在课程站点上的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),而不是“归零”,将其用作第二次测试的新基础(90 + 72 = 162)等。

有没有人上过这门课程或对如何解决这个问题有任何想法?

1 个答案:

答案 0 :(得分:1)

看起来他们不希望您手上累积点数。

我想IDE会多次调用playHand,您会将手得分保留在score变量中,该变量是全局得分(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")

或在score的开头重置为0 playHand