3

Hello I want to make a function that will use enhance(which just changes the word already made it) and print the new word in parts of a given number n. Example for S=test I should get (‘##t’, ‘#te’, ‘tes’, ‘est’, ‘st%’, ‘t%%’)

def enhance(S,n):
    S = "#"*(n-1)+S+"%"*(n-1)
    return S

def exploder(S,n):
    S = enhance(S,n)
    x=0
    for i in range (n <= len(S)):
        print(S[x:i])
        x=x+1


S="test"
n = 3
for n in range (0,n):
    print(exploder(S,n))
    n=n+1
print(exploder(S,n))
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
Alex
  • 49
  • 2
  • 4

2 Answers2

2

One immediate fix. Instead of:

  for i in range (n <= len(S)):

I think you want:

  for i in range(n, len(S) + 1):

That will give you values of i in the range n <= i < len(s).

Also, as Alex Hall suggested, change:

print(exploder(S,n))

To just:

exploder(S,n)

The exploder function was returning None. So that print is the source your spurious None outputs.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • This works but I get one more None print. Sorry I am new to python – Alex Mar 15 '17 at 09:25
  • 1
    @Alex just call `exploder`, don't `print` it, since you're printing what it returns which is nothing. `exploder` does the printing on its own. – Alex Hall Mar 15 '17 at 09:28
  • @Alex correctly identified the second problem with the code. The ``print(exploder(S,n))`` is where the ``None`` outputs were coming from. – Raymond Hettinger Mar 15 '17 at 09:31
1
def enhance(S, n):
    S = "#" * (n - 1) + S + "%" * (n - 1)
    return S

def exploder(S, n):
    S = enhance(S, n)
    for i in range(len(S)-n+1):
        print(S[i:i+n])

S = "test"
n = 3
exploder(S, n)

Output:

##t
#te
tes
est
st%
t%%
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • But this way I dont get a t%% print the last one. – Alex Mar 15 '17 at 09:25
  • @Alex oops, didn't notice. You just have to add one to the range of `i`, as I've [edited](http://stackoverflow.com/posts/42805509/revisions). – Alex Hall Mar 15 '17 at 09:27