1

I'm creating a prime number finding script, and I want to print what number is currently active.

def is_divisible(number: int, dividor: int) -> bool:
    float_number = number / dividor
    if float_number.is_integer():
        return True
    return False


def is_prime(number: int) -> bool:
    """
        Returns a bool, whether the a number is a prime.
    :param number: number
    :return: bool (True if it is a prime, False if it's not a prime)
    """
    tmp_numbers = set()

    for i in range(2, number):
        if any(is_divisible(i, n) for n in tmp_numbers):
            continue

        if is_divisible(number, i):
            return False

        tmp_numbers.add(i)

    return True


number = 99999999
for i in range(2, 90000):
    print("checking {}".format(i), end="\r")
    if is_prime(i) and is_prime(i + 4) and is_prime(i + 8):
        print(i)

When I terminate the script, I can see that line ("checking ...") but while it's executing I can't see it.

smci
  • 32,567
  • 20
  • 113
  • 146
Myzel394
  • 1,155
  • 3
  • 16
  • 40
  • 2
    If you are using windows it's normal because cmd prints only strings that end with a new line. It should work on linux. I don't know about macOS – Andrea Oggioni Aug 29 '19 at 14:28
  • I'm using the Pycharm IDE. Does this make a difference? – Myzel394 Aug 29 '19 at 14:29
  • What operativing system are you using? – Andrea Oggioni Aug 29 '19 at 14:30
  • Windows 10 64Bit – Myzel394 Aug 29 '19 at 14:33
  • Try on a different system or use the python IDLE. – Andrea Oggioni Aug 29 '19 at 14:35
  • It works in the python IDLE. Thank you! If you write an answer, I'll mark accept it and give you a vote. – Myzel394 Aug 29 '19 at 14:39
  • So this is an issue with Windows/DOS console not handling '\r` carriage returns properly. Why don't you just use the default line-ending, don't do `print(..., end="\r")`? – smci Aug 29 '19 at 14:41
  • Using `\r` carriage-return to get overwrite is an OS/console-specific trick that does not work universally. See [How can I overwrite/print over the current line in Windows command line?](https://stackoverflow.com/questions/465348/how-can-i-overwrite-print-over-the-current-line-in-windows-command-line) – smci Aug 29 '19 at 14:46
  • From the other question - basically `sys.stdout.flush()`. Also, `\r` works on every *mainstream* OS (i.e. Mac, Linux, Windows). – Wayne Werner Aug 29 '19 at 14:51
  • WayneWerner: or you can run `python -i` unbuffered. But having to hack in an OS-specific flush/command line is not acceptable, and **if you run without it, the stdout gets delayed for minutes/hours (or forever if the shell hangs/runs out of memory)**. So, we come back to warning people that '\r' is not reliably a universal overwrite character on all OSs/consoles/IDEs/shells. Especially not for progress messages. – smci Aug 29 '19 at 15:32
  • 1
    @AndreaOggioni, cmd.exe has nothing directly to do with Python's console or the console in general. In this regard, cmd.exe is no different from python.exe, or any other console application. The console is hosted by an instance of conhost.exe, which is shared by all processes that are attached to it. – Eryk Sun Aug 30 '19 at 18:54
  • @smci, I don't know which terminal people are using that Python chooses to fully buffer stdout -- probably some MSYS2 git-bash thing that's actually using a *pipe* for stdout (of course a pipe is fully buffered). But for the regular Windows console, Python uses line buffering for stdout, and none of these examples requires `stdout.flush()` or a flush via `print('...', end='\r', flush=True)`. – Eryk Sun Aug 30 '19 at 18:58

1 Answers1

0

Try this:

print('Checking : ' + str(i), end='\r')
Yash
  • 3,438
  • 2
  • 17
  • 33