Home > Mobile >  Function return 'None' Python
Function return 'None' Python

Time:12-29

Hi i am learning python on my own.

Task: Reverse word without affecting special characters

Example "abcd efgh" => "dcba hgfe" Example "a1bcd efg!h" => "d1cba hgf!e"

My problem: the function return None

Then i added this line: return reverse_text but it still return None

Can anyone show me where is my mistake is, please?

My code:

from string import punctuation
from string import digits


def reverse_text(str_smpl):
    sp = set.union(set(punctuation), set(digits))
    reverse_text.lst = []
    for word in str_smpl.split(' '):
        letters = [c for c in word if c not in sp]
        for c in word:
            if c not in sp:
                reverse_text.lst.append(letters.pop())
                continue
            else:
                reverse_text.lst.append(c)
        reverse_text.lst.append(' ')
    return reverse_text


if __name__ == '__main__':
    cases = [
        ("abcd efgh", "dcba hgfe"),
        ("a1bcd efg!h", "d1cba hgf!e"),
        ("", "")
    ]

    for text, reversed_text in cases:
        assert reverse_text(str_smpl) == reversed_text

    reverse_text(input('Input string '))
    print("".join(reverse_text.lst))

CodePudding user response:

The issue is that you are returning reverse_text which is the name of the function, so the function is returning a reference to itself (not what you want!).

Assigning properties to functions like you have with reverse_text.lst is not something I have really come across in Python and I would suggest you just use a new local variable named something like reversed_text_list to avoid confusion.

I think you also want to join the characters in the list together and return a string.

The following seems to be doing what I think you are trying to do:

def reverse_text(str_smpl):
    sp = set.union(set(punctuation), set(digits))
    reversed_text_list = []
    for word in str_smpl.split(' '):
        letters = [c for c in word if c not in sp]
        for c in word:
            if c not in sp:
                reversed_text_list.append(letters.pop())
                continue
            else:
                reversed_text_list.append(c)
        reversed_text_list.append(' ')
        reversed_text = ''.join(reversed_text_list)
    return reversed_text

CodePudding user response:

It returned error because you had defined reverse_text.lst but returned only reverse_text, the following code will work:-

from string import punctuation
from string import digits


def reverse_text(str_smpl):
    sp = set.union(set(punctuation), set(digits))
    lst = []

    for word in str_smpl.split(' '):
        letters = [c for c in word if c not in sp]

        for c in word:
            if c not in sp:
                lst.append(letters.pop())
                continue
            else:
                lst.append(c)

        lst.append(' ')

    return "".join(lst[:len(lst)-1])


if __name__ == '__main__':
    cases = [
        ("abcd efgh", "dcba hgfe"),
        ("a1bcd efg!h", "d1cba hgf!e"),
        ("", "")
    ]

    for text, reversed_text in cases:
        assert reverse_text(text) == reversed_text

    print(reverse_text(input('Input string ')))
  • Related