0

Firstly I apologize for being an absolute rookie at programming. My idea was moving the ball (o) until the end of the track so the ball should update its position every iteration but not printing a new line for each position but in a single line so I can see the ball moves along the track. This is my code

track=['o','.','.','.','.','.','.','.']
blank='.'
for i in range(1,len(track)):
    track[i]='o'
    track[i-1]=blank
    print(''.join(track))
Jim G.
  • 15,141
  • 22
  • 103
  • 166

2 Answers2

2

Just change your last line:

print(''.join(track), end='\r')
marcos
  • 4,473
  • 1
  • 10
  • 24
0

you can use the control char \r to return to the start of the line. and set the line end to be blank so it doesnt skip to a new line. thus each time you print it will overwrite the data.

import time
track=['o','.','.','.','.','.','.','.']
blank='.'
for i in range(1,len(track)):
    track[i]='o'
    track[i-1]=blank
    print(*track, sep='', end='')
    time.sleep(0.5)
    print("\r", end='')
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42