Python 非类型函数是如何工作的?

Python 非类型函数是如何工作的?,python,function,python-3.x,Python,Function,Python 3.x,为了为游戏创建update\u score函数,我正在开发一个返回NoneType的函数。我在Python3中搜索了一些关于这类函数的信息,据我所知,我应该做的是尝试在不打印的情况下创建它,但是返回NoneType,这到底意味着什么呢 我尝试使用另一个名为word\u score的函数对该函数进行编码,该函数似乎工作正常,但我被困在update\u score函数中 def word_score(word): """ (str) -> int Return the poi

为了为游戏创建
update\u score
函数,我正在开发一个返回
NoneType
的函数。我在Python3中搜索了一些关于这类函数的信息,据我所知,我应该做的是尝试在不打印的情况下创建它,但是返回
NoneType
,这到底意味着什么呢

我尝试使用另一个名为
word\u score
的函数对该函数进行编码,该函数似乎工作正常,但我被困在
update\u score
函数中

def word_score(word):
    """ (str) -> int

    Return the point value the word earns.

    Word length: < 3: 0 points
                 3-6: 1 point per character for all characters in word
                 7-9: 2 points per character for all characters in word
                 10+: 3 points per character for all characters in word

    >>> word_score('DRUDGERY')
    16
    >>> word_score('PEN')
    3
    >>> word_score('GRANDMOTHER')
    33
    """
    if len(word) < 3:
        return 0
    elif len(word) in range(3,7):
        return len(word)
    elif len(word) in range(7, 10):
        return len(word)* 2
    elif len(word) >= 10:
        return len(word) * 3



    return word_score


def update_score(player_info, word):
    """ ([str, int] list, str) -> NoneType

    player_info is a list with the player's name and score. Update player_info
    by adding the point value word earns to the player's score.

    >>> update_score(['Jonathan', 4], 'ANT')
    """

    return update_score(['player_info', word_score], 'word')
def word_分数(word):
“”“(str)->int
返回单词获得的分值。
字长:<3:0分
3-6:word中所有字符每字符1分
7-9:word中所有字符每个字符2分
10+:word中所有字符每个字符3分
>>>单词得分(“苦工”)
16
>>>单词分数(‘笔’)
3.
>>>单词评分(“祖母”)
33
"""
如果len(word)<3:
返回0
范围(3,7)内的elif len(字):
返回len(word)
范围(7,10)内的elif len(字):
返回长度(字)*2
elif len(word)>=10:
返回长度(字)*3
返回单词_分数
def更新分数(玩家信息,文字):
“”([str,int]list,str)->NoneType
玩家信息是包含玩家姓名和分数的列表。更新玩家信息
通过将积分值添加到玩家的得分中。
>>>更新大学分数(['Jonathan',4],'ANT')
"""
返回更新分数(['player\u info',word\u score],'word')

你看到这个函数有什么奇怪的地方吗?

update\u score
总是调用自身,所以永远不会返回。

非类型函数是一个在终止时不显式返回任何内容或显式返回无的函数。其中一个重要方面是函数返回。问题代码中的
update\u score()
无条件地调用自身,从而创建一个无限循环,直到出现异常停止整个脚本时才会终止

所以你不想这么做。这里是
update\u score()
的一个修改版本,它只更新传递给它的列表中的值,in-place,然后返回它。像那样返回它(或其他任何东西)并不是绝对必要的,因为
list
s是可变序列,所以您可能不需要我在末尾提供的
return player\u info
功能(从技术上讲,这是防止它成为
NoneType
功能)

注意,我还稍微优化了
word\u score()
函数

def word_score(word):
    """ (str) -> int

    Return the point value the word earns.

    Word length: < 3: 0 points
                 3-6: 1 point per character for all characters in word
                 7-9: 2 points per character for all characters in word
                 10+: 3 points per character for all characters in word

    >>> word_score('DRUDGERY')
    16
    >>> word_score('PEN')
    3
    >>> word_score('GRANDMOTHER')
    33
    """
    word_len = len(word)
    if word_len < 3:
        return 0
    elif word_len <= 6:
        return word_len
    elif word_len <= 9:
        return word_len * 2
    else: # word_len >= 10
        return word_len * 3

def update_score(player_info, word):
    """ ([str, int] list, str) -> [str, int] list

    player_info is a list with the player's name and score. Update player_info
    by adding the point value word earns to the player's score.

    >>> update_score(['Jonathan', 4], 'ANT')
    ['Jonathan', 7]
    """
    player_info[1] += word_score(word)
    return player_info

print(update_score(['Jonathan', 4], 'ANT'))  # -> ['Jonathan', 7]
def word_分数(word):
“”“(str)->int
返回单词获得的分值。
字长:<3:0分
3-6:word中所有字符每字符1分
7-9:word中所有字符每个字符2分
10+:word中所有字符每个字符3分
>>>单词得分(“苦工”)
16
>>>单词分数(‘笔’)
3.
>>>单词评分(“祖母”)
33
"""
单词_len=len(单词)
如果单词长度<3:
返回0
elif word_len['Jonathan',7]

A
NoneType
函数只是一个不显式返回任何内容(或显式返回None)的函数。我是否应该删除该返回并保留其余部分?其余部分似乎也不太有用。您能给出一个提示以便正确创建它吗?提前谢谢