-7

Hey, I'm trying to figure out how to input a space between every fifth letter in my program after the computer asks how many coins they would like. So this is what it looks like currently.

pile1_str = "pile 1: "
x="O"
y=1
while y <= pile1:   #pile1 being the number of chips the user inputs     
    pile1_str = pile1_str + x
    print(pile1_str)
    y= y+1

Here's what it's supposed to look like when you input, say 12 coins:

pile 1: OOOOO OOOOO OO

What can I do to insert a space in the string after every n-th (5th) character?

apxcode
  • 7,696
  • 7
  • 30
  • 41
TheAznHawk
  • 43
  • 1
  • 1
  • 6

2 Answers2

1
pile1_str = "pile 1: "
x="O"
y=1
while y <= pile1:
    pile1_str = pile1_str + x
    if y%1==0:
        pile1_str = pile1_str + " "
    print(pile1_str)
    y= y+1
galinette
  • 8,896
  • 2
  • 36
  • 87
1
length = 19
groupSize = 5
pile = 'O' * length
spacedPile = ' '.join(pile[i:i+groupSize] for i in xrange(0, len(pile), groupSize))
print spacedPile
Archie
  • 6,391
  • 4
  • 36
  • 44
  • Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Feb 28 '17 at 13:49