Hello World
Hello World

Reputation: 63

Error with adding strings onto empty string

I'm having a problem with my program that is supposed to reverse the string by using a function that takes the first word of a string and a function that prints the string without the first word.

def first_word(string):
    first_space_pos = string.find(" ")
    word = string[0:first_space_pos]
    print(word)

def last_words(string):
    first_space_pos = string.find(" ")
    words = string[first_space_pos+1:]
    print(words)

def reverse(string):
    words = string.count(" ") +1
    count = 1
    string_reversed = ""
    while count < words:
        string_reversed = first_word(string) + str(" ") + string_reversed
        string = last_words(string)
        count += 1
    print(string_reversed)

The error I'm getting is:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

and it's for this line:

string_reversed = first_word(string) + str(" ") + string_reversed

Any help is appreciated. Thanks.

Upvotes: 0

Views: 88

Answers (4)

Devansh Bansal
Devansh Bansal

Reputation: 177

Both of your methods first_word() & last_words() does not return anything and therefore a null value is created which cannot be added with a string. Use return instead of print and it will be good.

Upvotes: 0

R Nar
R Nar

Reputation: 5515

you are confusing the functionality of print with return.

print is a python function that will print whatever is in the brackets to whatever may be the stdout or another output stream if explicitly specified.

return is the statement that sends what you want back as a function value.

IE:

>>> def foo():
    print('foo')


>>> def bar():
    return 'bar'

>>> u = foo()
foo
>>> u
>>> type(u)
<class 'NoneType'>
>>> u = bar()
>>> u
'bar'
>>> type(u)
<class 'str'>

additionally, you can do what you want with the python str[::-1]

>>> def stringReverse(s):
    print(' '.join(s.split()[::-1]))


>>> stringReverse('Hello this is how to reverse a string')
string a reverse to how is this Hello

this is just a suggestion however as you may be trying to do a string reversal in a specific manner. as Prune said, your functions work perfectly fine if you replace print with return

Upvotes: 0

Prune
Prune

Reputation: 77850

You have confused print and return. print dumps a value to the output (your screen), but doesn't change any of the data in memory, and does not do anything further with the value. return sends it back as the function value.

As the complier warning tells me, none of your functions returns any value to the caller. Thus, you get "None" as the value, and your calling program burfs. Replace all those terminal print statements with return's, and your code works nicely:

def first_word(string):
    first_space_pos = string.find(" ")
    word = string[0:first_space_pos]
    return word

def last_words(string):
    first_space_pos = string.find(" ")
    words = string[first_space_pos+1:]
    return words

def reverse(string):
    words = string.count(" ") +1
    count = 1
    string_reversed = ""
    while count < words:
        string_reversed = first_word(string) + str(" ") + string_reversed
        string = last_words(string)
        count += 1
    return string_reversed

print reverse("Now is the time for all good parties to come to the aid of man.")

Output:

of aid the to come to parties good all for time the is Now 

Upvotes: 0

Andrey
Andrey

Reputation: 60075

first_word doesn't return anything, hence None value is produced and can't be used as + operand with string. Probably you want to return word.

Upvotes: 2

Related Questions