mayna
mayna

Reputation: 85

how to create a program to have an output so it repeats each letter once more as it goes down the word?

using python how can i get an out out to look like this dooggg so each letter it repeats once more. i have tried something that looks like this

# Program 21
def double(source):
  pile = ""
  for letter in source:
    pile = pile+letter+letter
    print pile
  print pile

but it comes out looking like

dd
ddoo
ddoogg

Upvotes: 1

Views: 49

Answers (2)

sw123456
sw123456

Reputation: 3459

Try...

def double(source):
    word = []
    for x in range(len(source)):
        word.append(source[x]*(x+1))
        print ''.join(map(str, word))


double("dog")

input()

and if you want it all on one line try this for python 2:

def double(source):
    word = []
    for x in range(len(source)):
        word.append(source[x]*(x+1))
    print ''.join(map(str, word)),


double("dog")

input()

and this for python 3:

def double(source):
    word = []
    for x in range(len(source)):
        word.append(source[x]*(x+1))
    print(''.join(map(str, word)), end="")


double("dog")

input()

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 113988

 "".join(l*i for i,l in enumerate(my_word,1))

I think should do it

Upvotes: 1

Related Questions