How to use the Time module together with the Sys module

Asked

Viewed 75 times

0

I have a problem using the time.Sleep module together with the sys module. You’re not taking the break I put on the team..

import random
import time
import sys


cores = {'fim': '\033[m', 'vermelho': '\033[31m', 'amarelo': 
'\033[33m', 'roxo': '\033[35m' }

print(20 * f'{cores["vermelho"]}-{cores["fim"]}{cores["amarelo"]}= 
{cores["fim"]}{cores["vermelho"]}-{cores["fim"]}', 
f'{cores["roxo"]}JOKEMPO!!!{cores["fim"]}', 20 * 
f'{cores["vermelho"]}-{cores["fim"]}{cores["amarelo"]}={cores["fim"]} 
{cores["vermelho"]}-{cores["fim"]}' )
print('')

escolha = int(input('''Escolha:

[1] - Pedra 
[2] - Papel
[3] - Tesoura

 '''))
 print('')

 sys.stdout.write(f'{cores["vermelho"]}JO{cores["fim"]}')
 time.sleep(1)

 sys.stdout.write(f'  {cores["amarelo"]}KEM{cores["fim"]}')
 time.sleep(1)

 sys.stdout.write(f'  {cores["vermelho"]}POW!!!{cores["fim"]}')
 time.sleep(1)

 print('')
 print('')

escolha_pc = random.randint(1, 3)

if escolha == escolha_pc:
  print('Sua escolha foi igual a do PC, vocês empataram!!!')
elif escolha == 1 and escolha_pc == 2:
  print('Você escolheu Pedra e o PC Papel. Papel ganha da Pedra, o PC 
  ganhou!!!')
elif escolha == 1 and escolha_pc == 3:
  print('Você escolheu Pedra e o PC Tesoura. Pedra ganha de Tesoura, 
  você ganhou!!!')
elif escolha == 2 and escolha_pc == 1:
  print('Você escolheu Papel e o PC Pedra. Papel ganha da Pedra, você 
ganhou!!!')
elif escolha == 2 and escolha_pc == 3:
  print('Você escolheu Papel e o PC Tesoura. Papel perde para Tesoura, 
o PC ganho!!!')
elif escolha == 3 and escolha_pc == 1:
  print('Você escolheu Tesoura e o PC Pedra. Tesoura perde para Pedra, 
  o PC ganhou!!!')
elif escolha == 3 and escolha_pc == 2:
  print('Você escolheu Tesoura e o PC Papel. Tesoura ganha de Papel, 
  você ganhou!!!')

print('')
print(22 * f'{cores["vermelho"]}-{cores["fim"]}{cores["amarelo"]}= 
{cores["fim"]}{cores["vermelho"]}-{cores["fim"]}', 
f'{cores["roxo"]}FIM{cores["fim"]}', 22 * f'{cores["vermelho"]}- 
{cores["fim"]}{cores["amarelo"]}={cores["fim"]}{cores["vermelho"]}- 
{cores["fim"]}')

Between JO, KEM and POW, it should have a break of 1s, but it doesn’t. I used the Sys module so that JO, KEM and POW would be side by side.

1 answer

1


Instead of sys.stdout.write, simply use the command print, but pass the optional parameter flush with the value of True.

For example:

print(f'{cores["vermelho"]}JO{cores["fim"]}', end='', flush=True)
time.sleep(1)

The reason you must have seen the use of sys.stdout.write somewhere else is that with the print old Python 2, was the only way to avoid line crossing, (or at least a space, if there was a comma at the end).

In Python 3, with the function print, the optional parameters sep, end, file, and flush allow complete control of printing without special syntax.

In fact, your problem could be solved without using the print - continuing with the sys.stdout.write - would only need after each write call the method flush in the archive - sys.stdout.flush() - but the use of print not only is it shorter, it already makes this call internally for you.

And finally, it’s not directly related to your question: I found out a little while ago how to make the ANSI color codes you are using work also on Windows, without needing settings and installations on the operating system - this way more people can use your code -

In windows, people should install the library colorama, with the command pip install colorama - and at the beginning of the programme the lines:

try:
    import colorama
    colorama.init()
except ImportError:
    pass

Browser other questions tagged

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