无法找出函数返回“无”的原因

时间:2017-11-04 20:31:46

标签: python-3.x

所以我正在尝试为类中编写一个函数,它接受一个字符串并遍历其中的每个字符,每次遇到'a''b'或'c'时它应该添加3 5和7 “值”变量(即a = 3b = 5且c = 7)。然后在字符串的末尾,我需要返回值%11的剩余部分。

这是我到目前为止所做的:

(ps.hash_func中的所有print语句都是这样,我可以看到发生了什么)

def hash_func(string):
    value=0
    string_length= range(len(string))
    for i in (string_length):
        current_charecter= string[i]
        print (current_charecter)
        if current_charecter== 'a':
            value=value + 3
            print (value)
        elif current_charecter== 'b':
            value=value + 5
            print (value)
        elif current_charecter== 'c':
            value=value + 7
            print (value)
        elif current_charecter!= 'a' or 'b' or 'c':
            value=value+0
        else:
            value=value%11
            print (value)



print(hash_func("banana")) #this is the call to the function that is given by the grader, it should equal 3

并且功能返回:

b
5
a
8
n
a
11
n
a
14
None

(它只返回没有额外打印语句的“无”)

所以我知道它正在添加值并正确地跳过不是b或c的字母,我似乎无法弄清楚“无”的来源。

据我所知,当一个函数或变量不包含任何内容时,返回none值,所以我不确定为什么在我的函数执行所有(大多数)正确后返回它。

如果有人能破译我的代码并告诉我我犯的愚蠢错误,我将非常感激!

2 个答案:

答案 0 :(得分:2)

您的问题的答案,您的函数不返回任何内容,因此默认返回None

另请注意您的代码错误,这里是解释

def hash_func(string):
    value = 0

    # It will iterate over the chars in the string and set ch to each char in
    # turn
    for ch in string:
        if ch == 'a':
            value += 3  # same thing as value = value + 3
        elif ch == 'b':
            value += 5
        elif ch == 'c':
            value += 7

        print(ch, value)

        # You don't need this and it's wrong also (note 1)
        # elif ch != 'a' or 'b' or 'c':
        #    value += 0

    # After you've iterated over the string, only then do you modulo it.
    # Otherwise you're performing a modulo every time you run into a character
    # that's not 'a', 'b' or 'c'
    return "Hash is " + str(value % 11)

print(hash_func("banana"))

注1 :如果您有if statement,则可以使用else匹配与之前条件不匹配的任何内容。如果您检查是否ch == 'a',则无需检查ch != 'a'

另外,做

ch == 'a' or X or Y

不符合你的想法。

它实际做的是

is ch equal to 'a'? Yes, ok condition is true
otherwise, is X true? or condition is true
otherwise, is Y true? or condition is true
otherwise condition is false

请参阅https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not

如果您想检查ch is one of 'a', 'b', or 'c')是否可以使用ch in ('a', 'b', 'c')

之类的内容

另外供您参考,

def better_hash_func(string):
    values = {'a': 3,
              'b': 5,
              'c': 7}
    value = 0
    for ch in string:
        if (ch in values.keys()):
            value += values[ch]
    return "Hash is " + str(value % 11)


def even_better_hash_func(string):
    values = {'a': 3,
              'b': 5,
              'c': 7}

    # We use the sum function to sum the list
    value = sum(
        [values[ch]                     # This is list comprehension
            for ch in string            # It create a list, by iterating over
                if ch in values.keys()  # some iterable (like a list)
        ]                               # This is very similar to a for loop,
    )                                   # but it takes a value you produce each
                                        # iteration and adds that
                                        # to a new list that is returned by the
                                        # list comprehension.
    return "Hash is " + str(value % 11)

有关列表理解的更多信息:http://www.pythonforbeginners.com/basics/list-comprehensions-in-python

答案 1 :(得分:0)

它返回None因为它不返回任何其他内容。停止打印返回值。

相关问题