How to change numbers on the same Python line of execution?

Asked

Viewed 310 times

2

Hello, I’m new to programming and I was doing a python countdown program. I did the code and it worked, but then I looked at the running program and the same using several lines just to change the number.... It would be possible to make it only change the count number, without appearing several lines in the execution?

There’s my code, I hope you understand what I mean...

from time import sleep

for c in range (10,0,-1):
    print('Aguarde, seu programa vai começar em ',end='')
    if c > 1:
        print(f'{c}',end='')
        if c==10:
            print(' segundos',end='')
        else:
            print('  segundos',end='')
    if c == 1:
        print(f'{c}',end='')
        print('  segundo ',end='')

    for i in range (0,3):
        if c == 0:
            break
        print(' .',end='')
        sleep(0.33)
    print('')

print('Seu programa esta rodando!')

1 answer

3

The terminal where print displays its contents is a special program that can have several capabilities - they are activated depending on the control codes you send inside the string itself in print.

So for example, to print colored text, multiple terminals but not the standard Windows terminal accept type sequences <ESC>[...m - are called ANSI sequences, (To print the ESC code, in a Python string we use its hexadecimal ASCII code, with the sequence \x1b.

These ANSI sequences include commands to reposition the cursor anywhere on the terminal screen - you choose the row and column where you want to print. Here is the reasonably complete documentation as well as the history of these sequences: https://en.wikipedia.org/wiki/ANSI_escape_code

Microsoft promised for this year a new terminal in Windows 10, which should replace the cmd that should have these sequences by default. Meanwhile, you can use a program called "Cmder" which is a Unix terminal experience on Windows - or the outsourced library "Colorama" to enable ANSI sequences in the cmd.

However, there is a simpler sequence, which works even on terminals that are with ANSI disallowed sequences, which is the backspacs character - Python has special encoding \b for the same that can be used in strings.

In addition to this sequence, it is important to customize the print call to (1), it does not send the " n" after printing and (2) do not wait for the " n" to update the line content - these two things are done by passing the parameters end="", flush=True to the print.

So try this example:

import time

print("Contagem regressiva")
for i in range(10, -1, -1):
    print(f"\b\b\b{i} ", end="", flush=True)
    time.sleep(0.5)

In the case of the 3 " b" move the cursor back 3 positions (if it reaches column 0 he stops), and the space " " after the "{i}" ensures that it erases the second digit (the 0 of the 10), when the count becomes a single digit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.