Blake Wilson
Blake Wilson

Reputation: 3

Cant figure out why function returns "None"

So I'm trying to write a function for class that takes a string and goes through each character in it, every time it encounters an 'a' 'b' or 'c' it should add 3 5 and 7 to the "value" variable (ie. a=3 b=5 and c=7). Then at the end of the string I need to return the remainder of value%11.

this is what I have so far:

(ps. all the print statements in the hash_func are just so I could see what was going on)

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

and the funtion returns:

b
5
a
8
n
a
11
n
a
14
None

(It just returns "None" w/o the extra print statements)

So I know it is adding the values and skipping the letters that arent a b or c correctly, I just cant seem to figure out where the "None" is coming from.

As I understand it the none value is returned when a function or variable doesn't contain anything so Im not sure why it's being returned after my function executes everything (mostly) correct.

If anyone can decipher my code and tell me the stupid mistake I'm making I would be very grateful!

Upvotes: 0

Views: 58

Answers (2)

francium
francium

Reputation: 2480

The answer to your question, your function returns nothing, hence None is returned by default.

Also note you code is wrong, here is the explanation

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"))

note 1: When you have an if statement you use the else to match on anything that doesn't match the previous conditions. If you check if ch == 'a' there is no need to check ch != 'a'.

Also, doing

ch == 'a' or X or Y

does not do what you think it does.

What it's actually doing is

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

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

If you wanna check if ch is one of 'a', 'b', or 'c') you can use something like, ch in ('a', 'b', 'c')

Additionally for your reference,

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)

for more about list comprehension: http://www.pythonforbeginners.com/basics/list-comprehensions-in-python

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

It returns None because it doesn't return anything else. Stop printing the return value.

Upvotes: 0

Related Questions