1 Replies - 1292 Views - Last Post: 25 November 2012 - 12:42 PM Rate Topic: -----

#1 Highest bidder   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 23-November 12

python pause and backspace

Posted 25 November 2012 - 09:37 AM

I have two problems and i thought it would be good to add them in the same topic.

Is there any possible way to delete something or write over it?
for example lets say i have a huge loop and i want to see its process so implement something like that:
print total_loop_times/100

(it is draft code)
therefore the program will print 0,32 for example but on the next loop i want it to write 0,33 on the same position!
is that possible?
and second: Is there any way to have the program paused for as long as i want without affecting the program?
i know that i can just use:
raw_input() #version 2.x
input() #version 3.x

but this way it will print a spare line... Is there any way to do the exact same thing with the down arrow for instance?

Is This A Good Question/Topic? 0
  • +

Replies To: python pause and backspace

#2 Python_4_President   User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: python pause and backspace

Posted 25 November 2012 - 12:42 PM

Here's a very simplistic approach to that. If you need something more complex and are on the Windows platform, you could look into The Console Module or the curses module (python standard lib) on linux or Tkinter (also in the standard lib) for cross platform functionality (but you'll be printing out to a text box)

import sys, os, time

if "linux" in sys.platform:
    def clear(): os.system("clear")
    timer = time.time
    def clear(): os.system("cls")
    timer = time.clock

def is_it_time(t):
        #lower the number if you plan on pausing/unpausing more than twice in a second.
	if t < 0.5:
                print "Quitting!"
#ctrl-c to pause/unpause, double-tap to quit
paused = False
current_index = 0
t = timer()
while True:
                #replace this loop with your own, but note the location of clear and sleep
		for i in range(current_index, 50):
			current_index = i
			print "At: {0}".format(i)
			time.sleep(0.5) if not paused else time.sleep(1000000)
                current_index = 0
	except KeyboardInterrupt:
		paused = not paused
		current_index += 1
		elapsed_t = timer() - t
		t = timer()

This post has been edited by Python_4_President: 25 November 2012 - 03:20 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1