0

Although I am defining my function as below:

import textwrap

def wrap(string, max_width):
    print(textwrap.fill(string, max_width))

if __name__ == '__main__':
    string, max_width = input(), int(input())
    result = wrap(string, max_width)
    print(result)

I am getting an error like:

*ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ
None*

Can you please help me to debug why this "None" or how is this value getting augmented with the output.

ANKAN
  • 3
  • 4

3 Answers3

1

You are printing twice. Once in the function, and then again the return value of the function. The None is coming from the second print.

Do this:

import textwrap

def wrap(string, max_width):
    return textwrap.fill(string, max_width) # return , don't print

if __name__ == '__main__':
    string, max_width = input().rstrip(), int(input())
    result = wrap(string, max_width)
    print(result)
rdas
  • 20,604
  • 6
  • 33
  • 46
0

you forgot to retun the result:

this should work:

import textwrap

def wrap(string, max_width):
    return(textwrap.fill(string, max_width))

if __name__ == '__main__':
    string, max_width = input(), int(input())
    result = wrap(string, max_width)
    print(result)
gekigek99
  • 303
  • 3
  • 17
0

wrap() does not have an explicit return. By default it returns None. So the value of result is None. Just get rid of the result variable and the following print() to fix it. Alternatively, change wrap() to return the result instead of printing it.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268