Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Changing a part of a string, but still displaying the other part.

So, I've got a bit stuck and I've found no answer.

What I actually want is something like this:

Code
Time left: 4
Time left: 3

But I don't want the 'Time Left' to be printed everytime but just for the number (4) to change.

How can I do this with python? Or should I clear the screen everytime and reprint the string?

Cheers!
/thread

[Image: M6MscUn.gif]

So, you want the console to print 'Time Left: n' on a single line and for the line to 'update' to the console with a new value for n?
Try this,
Code
import time, sys

for i in range(100):
    time.sleep(1)
    sys.stdout.write("\rTime Left:%d%%" %i)
    sys.stdout.flush()
[Image: 2owATV2.gif]
If you're sneaky you can just print \d (the delete character) then print the correction.
Does the entire thing have to be 1 string?
For example would this be acceptable:
PHP Code

<?php 
import time
import sys

x 
= 100; 
for 
i in range(0, 100):
  
time.sleep(1)
  print(
"Time left: " + x);
  
x = x -1; 

EDIT: Just realized this is wrong, x should be a string when being used by the function print. Since OP has said this script is not the one he is looking for please disregard this code.
(Sep 11, 2013 06:49 AM)Logicmorg Wrote:  Does the entire thing have to be 1 string?
For example would this be acceptable:
PHP Code

<?php 
import time
import sys

x 
= 100; 
for 
i in range(0, 100):
  
time.sleep(1)
  print(
"Time left: " + x);
  
x = x -1; 

That will just print the string over and over after a second.
Like this:

Code
Time left: 4
Time left: 3

I don't want that. I just want the number to change but for the 'time left' to be on the same line.
/thread

[Image: M6MscUn.gif]

use string specifiers with a loop should handle that for you?
wait I see what your asking now...
(Sep 11, 2013 07:39 AM)๖ۣۜAlexander Wrote:  That will just print the string over and over after a second.
Like this:

Code
Time left: 4
Time left: 3

I don't want that. I just want the number to change but for the 'time left' to be on the same line.

Okay I see what you are saying ( I think )
You want to display
Time left: 4
then after second goes by the 4 is then replaced with a 3, is this correct?
(Sep 11, 2013 08:12 AM)Logicmorg Wrote:  Okay I see what you are saying ( I think )
You want to display
Time left: 4
then after second goes by the 4 is then replaced with a 3, is this correct?

Yea, that's correct! All on the same line :)
/thread

[Image: M6MscUn.gif]

There is a really simple method you could use if you wish.

Code
import time
for i in range(100):
    time_left = 100 - i
    time.sleep(1)
    print("You have {0} seconds left.".format(time_left))

This is not really a very good way about doing this but I though I should provide a way using very simple code. Not this is not the best or even a good way of doing this. Also this code is in python 3.x format.

--

Just took a look through the later parts of the thread and I see that this is not really what you want. I will try and think of a way to do what you want gimmie a sec.
(Sep 11, 2013 08:22 AM)๖ۣۜAlexander Wrote:  Yea, that's correct! All on the same line :)
I did this for you earlier... Check at the top.
[Image: 2owATV2.gif]
(Sep 11, 2013 09:49 AM)ßlack мesa Wrote:  I did this for you earlier... Check at the top.

I don't have access to pc now, so I'm just taking suggestions which I can use later when I'm on PC :)
/thread

[Image: M6MscUn.gif]

(Sep 11, 2013 10:21 AM)๖ۣۜAlexander Wrote:  I don't have access to pc now, so I'm just taking suggestions which I can use later when I'm on PC :)

Code
import time
import sys

for i in range(4, 0, -1):
    print('\rTime left:', i, end='')
    sys.stdout.flush()
    time.sleep(1)

There you go, change the range accordingly. I would also like to note that this wont work in the idle I don't think. It should be ran how a user would run it.

Here is the version of the code for 2.x

Code
import time
import sys

for i in range(4, 0, -1):
    print '\rTime left:', i,
    sys.stdout.flush()
    time.sleep(1)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by dawid17Oct 17, 2020 05:22 AM 1 137 Oct 17, 2020 05:22 AM
Last Post: saltycracker
[.NET] by Ferrari F40Dec 7, 2019 12:07 PM 1 255 Dec 7, 2019 12:07 PM
Last Post: itcorp
[C] by MordiciaNov 15, 2019 07:21 PM 3 211 Nov 15, 2019 07:21 PM
Last Post: Mordicia