renix1

Count words in python 2.x

Dec 20th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. import sys
  4. import time
  5.  
  6. ver = '0.0.2-a'
  7.  
  8. if len(sys.argv) > 1:
  9.     if sys.argv[1] == '-v':
  10.         print('Versão: %s' % (ver))
  11.         print('\t\t\t\tExemplos:')
  12.         print('Se versão contém \'a\' (é alpha), \'b\' (é beta) ou \'rc\' (é release-candidate).')
  13.         sys.exit(0)
  14.     else:
  15.         print('Argumento não é valido. Desculpe-nos!')
  16.  
  17.  
  18. def loading(msg, tempo, pular_linha=False):
  19.     '''
  20.        FAZ O PROCESSO DE "LOADING"
  21.    '''
  22.     if pular_linha == 1:
  23.         print('\n%s' % (msg), end='')
  24.     else:
  25.         print('%s' % (msg), end='')
  26.     for x in range(tempo):
  27.         sys.stdout.write('.')
  28.         sys.stdout.flush()
  29.         time.sleep((tempo / 5.8) / 12)
  30.     print('')
  31.  
  32.  
  33. def contador_palavras(texto):
  34.     '''
  35.        LITERALMENTE CONTA AS PALAVRAS
  36.    '''
  37.     try:
  38.         loading('Carregando', 5)
  39.         texto = len(texto.split())
  40.         if texto > 1:
  41.             print("O texto tem %d palavras" % (texto))
  42.         else:
  43.             print("O texto tem %d palavra" % (texto))
  44.     except KeyboardInterrupt:
  45.         loading('Saindo', 3, True)
  46.         sys.exit(0)
  47.     except Exception:
  48.         print('exception', Exception)
  49.  
  50.  
  51. def main():
  52.     '''
  53.        EXECUTA TODAS AS FUNÇÕES ANTERIORES
  54.    '''
  55.     try:
  56.         texto = input("Digite um texto qualquer: ")
  57.         if len(texto) < 1:
  58.             pass
  59.         else:
  60.             contador_palavras(texto)
  61.     except KeyboardInterrupt:
  62.         loading('Saindo', 3, True)
  63.         sys.exit(0)
  64.  
  65.  
  66. while 1:
  67.     main()
Advertisement
Add Comment
Please, Sign In to add comment