0

How do I make:

c=5
for i in range(10):
    print(c)
    c+=1

so that instead of printing across several lines like:

5
6
7
8
9
10
11
12
13
14

In each loop it will go:

5 

Then when it loops again it will overwrite the existing line with:

6

How do I go about that?

Nishant
  • 20,354
  • 18
  • 69
  • 101
  • 1
    Use `yield` if you want to pause and resume the function. But it won't be like "in each loop, it will print", but instead "each time you call the generator it will resume the loop". – Nishant Dec 23 '17 at 06:19
  • @Nishant Can you show with example? –  Dec 23 '17 at 06:21
  • 4
    It sounds like you're talking about _overwriting_ the output on a line. In that case, look into something like the *curses* library: https://docs.python.org/3/howto/curses.html – jrd1 Dec 23 '17 at 06:21
  • Do you want to overwrite the existing line? If so you need to modify the question. – Nishant Dec 23 '17 at 06:24
  • @jrd1 Yeah I want to print single digit on each loop. When I try and install curses it gets no module: apparently "no direct Python for Windows port of curses exists." –  Dec 23 '17 at 06:26
  • @geeg: On the documentation for `curses`: _The Windows version of Python doesn’t include the curses module. A ported version called UniCurses is available. You could also try the Console module written by Fredrik Lundh, which doesn’t use the same API as curses but provides cursor-addressable text output and full support for mouse and keyboard input_ – jrd1 Dec 23 '17 at 06:28
  • Okay @geeg. It looks like a duplicate then! Check the question linked. – Nishant Dec 23 '17 at 06:28
  • @Nishant A large percentage of those solutions rely on time.sleep which is not good practise. –  Dec 23 '17 at 06:34

3 Answers3

3

In python 3.3, you can do like below

print(c , end="\r", flush=True)
sinsuren
  • 1,745
  • 2
  • 23
  • 26
  • this prints 14 for me ha –  Dec 23 '17 at 06:42
  • @geeg I have mentioned that above method would work only for python 3.3. Please check your python version. – sinsuren Dec 23 '17 at 06:49
  • I use Python 3.6.3. Isn't 3.3 an older python version? I'd imagine downgrading might cause issues with other libraries. –  Dec 23 '17 at 06:53
  • I have also tested above code for python 3.6.1 – sinsuren Dec 23 '17 at 06:54
  • I think that you should clearly highlight that this only work in Python 3.3, because when you see that in Python3.3 works, you instantly think that this will work in higher versions. – vishes_shell Dec 23 '17 at 07:39
  • @geeg 14 is the last value to be get printed in your case. If you want to see the change in value. please put sleep of few seconds after every iteration. – sinsuren Dec 25 '17 at 14:20
-1

To print text with a space instead of a newline at the end you can use the end parameter.

print("text", end=" ")

in python2 this can be achieved with a trailing comma:

print "text",

for Example,

c = 5
for i in range(10):
    print c,
    c += 1
Azsgy
  • 3,139
  • 2
  • 29
  • 40
Mike Pinkman
  • 169
  • 9
-1

You can use carriage return ("\r") to do the trick:

import time

c = 5
for i in range(10):
    print(c, end='\r')
    c += 1
    time.sleep(1)
sudormrfbin
  • 618
  • 8
  • 16