python 函数调用未按预期打印
python function call not printing as expected
所以我有 2 个函数 - displayHand(hand) 和 calculateHandlen(hand)
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
另一个函数中有一个循环依赖于上述函数 -
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: ')
playHand()函数调用如下:
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:
但是,它显示以下内容:
n e t a r i i
Current Hand: None
Enter word, or a "." to indicate that you are finished:
不知道我哪里错了。
注意:我不能对前两个函数进行任何更改。
displayHand()
没有 return 任何东西,所以默认的 None
是 returned。当你调用它时,它会直接打印输出。
如果不允许更改displayHand()
,则需要先打印标签,然后调用函数:
print("Current Hand: ", end='')
displayHand(hand)
end=''
删除换行符 print()
通常会写。
所以我有 2 个函数 - displayHand(hand) 和 calculateHandlen(hand)
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
另一个函数中有一个循环依赖于上述函数 -
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: ')
playHand()函数调用如下:
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:
但是,它显示以下内容:
n e t a r i i
Current Hand: None
Enter word, or a "." to indicate that you are finished:
不知道我哪里错了。
注意:我不能对前两个函数进行任何更改。
displayHand()
没有 return 任何东西,所以默认的 None
是 returned。当你调用它时,它会直接打印输出。
如果不允许更改displayHand()
,则需要先打印标签,然后调用函数:
print("Current Hand: ", end='')
displayHand(hand)
end=''
删除换行符 print()
通常会写。