2

I have a python program which has some variables that change over time. I want to print them to the screen, without printing in new line. For example:

for i in range(1,10):
    v1 = i
    v2 = i+1
    v3 = i+2

and while the program is running I want that the display will show:

variable 1 value = v1  #v1 changes over time
variable 2 value = v2  #v2 changes over time
variable 3 value = v3  #v3 changes over time

by overwriting the previous printed values of v1 v2 and v3

can it be done? I know it can be done with one printed line using '/r',, however this time I want to print more lines...

miltone
  • 4,416
  • 11
  • 42
  • 76
TomE8
  • 546
  • 1
  • 4
  • 14
  • Possible duplicate of [Replace console output in python](http://stackoverflow.com/questions/6169217/replace-console-output-in-python) – CoMartel Feb 04 '16 at 08:46
  • Possible duplicate of [clear terminal in python](http://stackoverflow.com/questions/2084508/clear-terminal-in-python) – Brendan Abel Feb 04 '16 at 08:53

2 Answers2

1

You could do the following (but this may not work in all displays), based on this other answer:

import os
for i in range(1,10):
    # First clear the screen
    os.system('cls' if os.name == 'nt' else 'clear')
    print('variable 1 value = %s' % i)
    print('variable 2 value = %s' % i+1)        
    print('variable 3 value = %s' % i+2)
Community
  • 1
  • 1
Nander Speerstra
  • 1,496
  • 6
  • 24
  • 29
0

If you want to print values in one line, you may:

for i in range(1, 10):
    print 'v1 = {0}, v2 = {0}, v3 = {0}'.format(i, i + 1, i + 2)

This link should explainsomething. Python Input Output

Jarek Szymla
  • 79
  • 1
  • 12