Can you write all the paragraphs that the user put only after the loop in sequence? and justifiably?

0

Can you write all the paragraphs that the user put only after the loop in sequence? and justifiably?

import textwrap

numero_paragrafos = int(input('Quantos parágrafos tem o texto?\n'))

for c in range(1, numero_paragrafos +1):
    paragrafo = str(input(f'\n parágrafo {c}: \n'))
    print(textwrap.fill(paragrafo, width=40))
    
asked by anonymous 10.10.2018 / 20:49

1 answer

1

You asked two questions in one.

  

Can you write all the paragraphs that the user put only after the loop in sequence?

So I understand, you just want to write after the loop ends, so you need to use a variable to store the result:

paragrafos = [] # lista para armazenar os paragrafos lidos
for c in range(1, numero_paragrafos +1):
    paragrafo = str(input(f'\n parágrafo {c}: \n'))
    paragrafos.append(paragrafo)

# apos ler todos os paragrafos, imprima o resultado
for paragrafo in paragrafos:
    print(textwrap.fill(paragrafo, width=40))
  

and justifiably?

You can use this function justifica , it works like textwrap.wrap() but it justifies the text:

def justifica(texto, tamanho):
    for linha in textwrap.wrap(texto.replace('\n', ''), tamanho):
        por_palavra, sobra = divmod(tamanho - len(linha), linha.count(' '))
        por_palavra *= ' '; sobra *= [' ']
        yield ' '.join(palavra + por_palavra + (sobra.pop() if sobra else '') 
            for palavra in linha.split(' ')).rstrip()

The way it works is by calling textwrap.wrap() and then completing each line with spanned spaces until it gets the requested size:

To use in your code, just put it print line:

print('\n'.join(justifica(paragrafo, 40)))
    
10.10.2018 / 21:25