python函数调用不按预期打印

时间:2018-03-10 17:49:14

标签: python-3.x dictionary function-call

所以我有2个函数 - displayHand(hand)和calculateHandlen(hand)

<a *ngIf="hasValue()" (click) = "toggle()?"></a> // I would like to invoke toggle() function when <a> element is clicked.
<my-app></my-app>

另一个函数中有一个依赖于上述函数的循环 -

def displayHand(hand):
    """
    Displays the letters currently in the hand.

    For example:
    >>> displayHand({'a':1, 'x':2, 'l':3, 'e':1})
    Should print out something like:
       a x x l l l e
    The order of the letters is unimportant.

    hand: dictionary (string -> int)
    """
    for letter in hand.keys():
        for j in range(hand[letter]):
             print(letter,end=" ")       
    print() 

def calculateHandlen(hand):
    """ 
    Returns the length (number of letters) in the current hand.

    hand: dictionary (string-> int)
    returns: integer
    """
    handLen = 0
    for i in hand:
        handLen = handLen + hand.get(i,0)

    return handLen

playHand()的函数调用如下:

def playHand(hand, wordList, n): 
"""
   hand = dictionary 
   wordList = list of valid words
   n = an integer passed while function call  
"""
    totalscore = 0
    while(calculateHandlen(hand)>0):
        print("Current Hand: " +str(displayHand(hand)))

        newWord = input('Enter word, or a "." to indicate that you are finished: ')

我希望输出为:

wordList = loadWords() #loadWords could be a list of words
playHand({'n':1, 'e':1, 't':1, 'a':1, 'r':1, 'i':2}, wordList, 7)

但是,它显示以下内容:

Current Hand: n e t a r i i 
Enter word, or a "." to indicate that you are finished:

不知道我哪里出错了。

注意:我不允许对前两个功能进行任何更改。

1 个答案:

答案 0 :(得分:0)

displayHand()不会返回任何内容,因此会返回默认的None。当你调用它时,它会直接输出输出。

如果您不允许更改displayHand(),则需要首先打印标签,然后调用该功能:

print("Current Hand: ", end='')
displayHand(hand)

end=''删除了通常会写的换行符print()