Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.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 代码中不必要的部分-“;“已加载单词列表”;最后_Python - Fatal编程技术网

Python 代码中不必要的部分-“;“已加载单词列表”;最后

Python 代码中不必要的部分-“;“已加载单词列表”;最后,python,Python,有一个快速的问题要检查。我正在做一个文字游戏,想找出一手牌的点数。我已经得到了大部分正确的代码,它运行良好。但是,我一直在底部重复代码的一部分,我想知道如何删除它?代码如下: def play_hand(hand, wordlist): """ Allows the user to play the given hand, as follows: * The hand is displayed. * The user may input a word.

有一个快速的问题要检查。我正在做一个文字游戏,想找出一手牌的点数。我已经得到了大部分正确的代码,它运行良好。但是,我一直在底部重复代码的一部分,我想知道如何删除它?代码如下:

def play_hand(hand, wordlist):

    """
    Allows the user to play the given hand, as follows:

    * The hand is displayed.

    * The user may input a word.

    * An invalid word is rejected, and a message is displayed asking
      the user to choose another word.

    * When a valid word is entered, it uses up letters from the hand.

    * After every valid word: the score for that word is displayed,
      the remaining letters in the hand are displayed, and the user
      is asked to input another word.

    * The sum of the word scores is displayed when the hand finishes.

    * The hand finishes when there are no more unused letters.
      The user can also finish playing the hand by inputing a single
      period (the string '.') instead of a word.

      hand: dictionary (string -> int)
      word_list: list of lowercase strings

    """
    print "Current Hand:", display_hand(hand)
    word = raw_input('Enter word, or a "." to indicate that you are finished:')
    handScore = 0

    while list(word) != ['.']:
        if is_valid_word(word, hand, wordlist) == True:
            score = get_word_score(word,n)
            handScore = handScore + score
            print word, "earned", score, "points. Total:", handScore, "points"
            hand = update_hand(hand, word)

            if calculate_handlen(hand) == 0:
                    print "Your hand has finished."
                    print "Your final score is", handScore
                    break
            else:
                print "Current hand:", display_hand(hand)
                word = raw_input('Enter word, or a "." to indicate that you are finished:')

        else:
            print "Invalid word, please try again."
            word = raw_input('Enter word, or a "." to indicate that you are finished:')


    if list(word) == ['.']:
        print "Total score: %d points" %(handScore)


n = 7
hand = deal_hand(n)
play_hand(hand, load_words())
下面是load_words()的代码:

我得到的回报是:

Loading word list from file...
   83667 words loaded.
Current Hand: a i j m n w v
None
Enter word, or a "." to indicate that you are finished:jam
jam earned 36 points. Total: 36 points
Current hand: i n w v
None
Enter word, or a "." to indicate that you are finished:win
win earned 18 points. Total: 54 points
Current hand: v
None
Enter word, or a "." to indicate that you are finished:v
Invalid word, please try again.
Enter word, or a "." to indicate that you are finished:.
Total score: 54 points
Loading word list from file...
   83667 words loaded.
如您所见,在底部有一个额外的“加载文件中的单词列表…加载了83667个单词。”它不应该在那里。有人知道我的代码出了什么问题吗

非常感谢! Andrea

也许load_words()函数是从问题中没有看到的其他函数中再次调用的

如果删除play_hand()并在此处显示输出会怎么样


注意:您始终可以使用pudb之类的调试器来检查程序流。

您可以发布完整的代码,包括
deal\u hand()
?这是edX类分配吗?“从6.00x课程工作人员那里,请不要参与此类作弊。首先,这违反了荣誉守则。其次,你们中少数人的行为给所有edX员工和学生带来了不好的印象。关于堆栈溢出,请不要寻求家庭作业帮助。使用我们的论坛,当我们的论坛关闭时,遵循荣誉代码,做你自己的工作。“我并没有把这当作一门课程,我实际上是去了麻省理工学院的开放式课程网站下载了材料。这是2011年春季6.00课程。我从来不知道有什么荣誉守则。这是否意味着我不应该在麻省理工学院开放式课程上获得帮助?
Loading word list from file...
   83667 words loaded.
Current Hand: a i j m n w v
None
Enter word, or a "." to indicate that you are finished:jam
jam earned 36 points. Total: 36 points
Current hand: i n w v
None
Enter word, or a "." to indicate that you are finished:win
win earned 18 points. Total: 54 points
Current hand: v
None
Enter word, or a "." to indicate that you are finished:v
Invalid word, please try again.
Enter word, or a "." to indicate that you are finished:.
Total score: 54 points
Loading word list from file...
   83667 words loaded.