-1

example output estimate of pi:

3.13658765

then a new better estimate comes along. Say, 3.1416232.

So I find the first character in the old estimate that doesn't match the new. In this case, it is the 4th character.

Question: Is there a way to delete the 4th character in console(and then repeat this until all characters after 3.1 are gone) so that I can then print the new, better values for each of those characters?

Note: I don't want to delete everything and console and then reprint, as this would get considerably slower as the number of digits increases.

user236529
  • 115
  • 1
  • 8
  • 2
    Possible duplicate of [python print one line same space](https://stackoverflow.com/questions/14789930/python-print-one-line-same-space) – Sean Breckenridge May 11 '18 at 02:41
  • 5
    It is possible. But it's a lot easier to keep track of what you printed to the console than to read characters from the console. It's easier still to just overwrite all the characters, whether they're the same or not. And it's much, much easier to not even think about it as a character-addressable console, and just print a CR without NL to whatever stdout is, and rewrite the whole line. – abarnert May 11 '18 at 02:46
  • No, my question involves deleting from console. – user236529 May 11 '18 at 02:46
  • abarnert I plan to compare the charcters without reading console, but then deleting part of the printed output. I don't want to delete everything that's been printed and then reprint, because reprinting a billion digits would be slower than deleting a few and reprinting them. – user236529 May 11 '18 at 02:49
  • 4
    A billion digits don't fit on a line, or even a full-screen terminal window on a 4K monitor with a tiny font, so that issue will never come up in the first place. – abarnert May 11 '18 at 02:51
  • 2
    Anyway, if you really want to do this, you need to tell us what kind of console you're talking about. For Windows, [`msvcrt`](https://docs.python.org/3/library/msvcrt.html) has some limited support for console stuff, but you probably want the more complete wrapper in `pywin32` or a third-party lib like `conio`. For *nix, if you can trust ncurses being present and termcap being correct, you want [`curses`](https://docs.python.org/3/library/curses.html). For anything else, you want… something else. Anyway, they all have a bit of a learning curve, and I don't know of a good tutorial to point at. – abarnert May 11 '18 at 02:53
  • I don't want to put all the digits on 1 line. – user236529 May 11 '18 at 05:47

2 Answers2

0

It sounds like you want something like:

best_est = None
while True:
    est = some_estimate_method() # assuming this is a 'str'
    if not best_est:
        best_est = est
    else:
        if (float(est) > float(best_est)):
            best_est = est
    print(best_est, end = '\r')''

Depending on the directional relation of the estimate (i.e. is less better or is more better), you would have to change the inequality to accommodate.

However, if we look at your example:

# initial print
3.13658765
# get new estimate (3.1416232)
# overwrite the print
3.1416232

You originally wanted to "re-print" 3.1 then print all the new characters in the new estimate. That begs to ask the question why not just print the whole new estimate?

Since the entire value of the new estimate is greater, that means the comparison you are looking for (starting at index 4 and beyond) has already be checked when using < or > for inequality. Simply calling a print(est, end = '\r') returns the console cursor to the beginning of the line, effectively overwriting the value each time.

pstatix
  • 3,611
  • 4
  • 18
  • 40
  • To answer your question - "Note that I don't want to delete everything and console and then reprint, as this would get considerably slower as the number of digits increases." – user236529 May 11 '18 at 05:43
-1

If you just need to overwrite your last line, you can do something like this. The "\r" means instead of a newline, a return carriage will be printed, moving you back to the start of the same line to overwrite

print("3.1515", end="\r")
print("3.1415")

Furthermore, if you want to do tricky things, you could look into ANSI escape codes, these will let you print to any arbitrary location, although they aren't the most portable. These will also let you do fun things like setting the colour.

For example print("\033[31;1HX") would print an X to the 31's column and 1st row. To delete a character, you could simply print a space over top (print("\033[31;1H ")).

This may not work on all terminals

http://ascii-table.com/ansi-escape-sequences.php

user1762507
  • 772
  • 9
  • 32