-1

In this question, I have to wrap a text according to the given value of width. Everything is going well upto the last part where the program prints "None" in the end.

I tried to make a new list and appending but that did not work out well. Here's the code:

import textwrap

def wrap(string, max_width):
    i = max_width
    j=0
    length = len(string)
    while j<length:    
        word = string[j:i]
        i = i+max_width
        j = j + max_width
        print(word)
string, max_width = input(), int(input())
result = wrap(string, max_width)
print(result)

The aim is to make the proper function. Everything is fine until the program prints "None" in the end.

Sample Input ABCDEFGHIJKLIMNOQRSTUVWXYZ 4

Sample Output
ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ

MY OUTPUT:
ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ
None

  • there is notthing inherently wrong with printing "None" at the end. Your code is doing what it says. is this for an assignment question or homework? – Preet Kukreti Jun 09 '19 at 17:04
  • It's homework. I am not supposed to get "None" in the end. – Gautum Subhash Jun 09 '19 at 17:06
  • 1
    Your output cannot be as described. It will be "ABCD". Please provide the actual code and actual output. – jarmod Jun 09 '19 at 17:13
  • I am so sorry. There was an error in the code that didn't reproduce my error. I corrected it. – Gautum Subhash Jun 09 '19 at 17:32
  • 1
    After your edit, the wrap() function prints each 4-character substring but does not explicitly return any value, which is OK except that you have assigned the return value to result and then printed result. In Python, when you don't explicitly return a value, the implicit value of None will be used. And you print that. Don't print it. In fact don't even assign the return value to result, because there is no return value. – jarmod Jun 09 '19 at 17:42

1 Answers1

0

A function is DONE when you return from it - the while loop is pointless - if it enters the while loop it returns a word - it does not loop.

def wrap(string, max_width):
    i = max_width
    j=0
    length = len(string)
    while j<length:    
        word = string[j:i]
        i = i+max_width
        j = j + max_width
        return word             # leaves as soon as it reaches this
    # if j not < length returns None implicitly

If it does not enter the while loop you return nothing, hence your function returns None implicitly.


If you want to return multiple results from your fuction you can make it a generator and yield results:

def wrap(text,width):
    start = 0
    lentext = len(text)
    while start < lentext:  # while still stuff to yiled
        yield text[start:start+width]
        start += width   # next time start +width further to the right

string, max_width = "ABCDEFGHIJKLIMNOQRSTUVWXYZ", 4

print(*wrap(string,max_width), sep="\n")

Output:

ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ

You can wrap your text using a list comprehension:

string, max_width = "ABCDEFGHIJKLIMNOQRSTUVWXYZ", 4
result = [string[i:i+max_width] for i in range(0,len(string),max_width)]


print(*result, sep="\n")

Output:

ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ

The list comp solution is covered in more detail here: How do you split a list into evenly sized chunks? - list and strings are "similar" as in: both are iterables.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69