I’m posting my answer as a complement to reply by @Woss. Your question specifies that you want to handle your result string to have a maximum of 100 so that your content fits on the screen.
My answer approaches the problem differently, so I want to leave it here as an alternative, so that some future visitor has other options of solution to the same problem.
Instead of limiting the number of characters per line, you can limit how many terms per line you want and show them as "columns".
Code
from itertools import cycle
a1, r, ter = 1, 2, 30
# nosso "paginador"
itens_por_linha = 6
ends = [''] * (itens_por_linha - 1)
ends.append('\n')
# ends = ['', '', '', '', '', '\n']
for item, end in zip(range(a1, r * ter, r), cycle(ends)):
print(f"{item: >5d}", end=end)
See rotating on Repl.it
Upshot
1 3 5 7 9 11
13 15 17 19 21 23
25 27 29 31 33 35
37 39 41 43 45 47
49 51 53 55 57 59
Explanation of the code
range(start, stop, step)
(#Docs)
As you yourself have already used in your code, I use the range to generate the P.A. Being:
a1
the parameter start
;
r * ter
will be the stop
to ensure that the size of the range generated has exactly N elements defined by the variable ter
;
r
will be the step
a1, r, ter = 1, 2, 10
pa = list(range(a1, r * ter, r))
print(pa)
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
print(len(pa))
# 10
print(*objects, end='\n')
(#Docs)
In my solution, I use the parameter end
that the function print
accepts to control when I must break the line.
for i in range(3):
print(i)
# Resultado:
# 0
# 1
# 2
for i in range(3):
print(i, end=" ")
# Resultado:
# 0 1 2
zip(*iterables)
(#Docs)
The function zip
is used to traverse "parallel" N. Example:
nums = [1, 2, 3]
letras = ['A', 'B', 'C']
simbolos = ['!', '@', '#', '$']
for num, letra in zip(nums, letras):
print(f"{letra}{num}", end=', ')
# A1, B2, C3
for num, letra, simb in zip(nums, letras, simbolos):
print(f"{letra}{num}({simb})", end=', ')
# A1(!), B2(@), C3(#)
Note that zip
always stop iterating in its shortest iterable, in the example above simbolos
has size 4, but iteration for after the 3rd item because nums
and letras
have only 3 items.
You can use the zip
outside the for
, for the return of zip
is an eternal too. See:
nums = [1, 2, 3]
letras = ['A', 'B', 'C']
simbolos = ['!', '@', '#', '$']
lista_1 = list(zip(nums, letras))
lista_2 = list(zip(nums, letras, simbolos))
print(lista_1)
# [(1, 'A'), (2, 'B'), (3, 'C')]
print(lista_2)
# [(1, 'A', '!'), (2, 'B', '@'), (3, 'C', '#')]
itertools.cycle(iterable)
(#Docs)
In my solution I use the cycle
to create an infinite repetition of the characters I want to use as end
of print
. See a simple example:
from itertools import cycle
abc = cycle("ABC")
nums = range(1, 6)
for letra, num in zip(abc, nums):
print(f"{letra}{num}", end=", ")
# A1, B2, C3, A4, B5,
par_impar = ('par', 'ímpar')
nums = range(5)
lista = list(zip(nums, par_impar))
print(lista)
# [(0, 'par'), (1, 'ímpar'), (2, 'par'), (3, 'ímpar'), (4, 'par')]
String formatting
In the solution print I use formatting >5d
, which means:
print(f"{item: >5d}")
# │││└── "d": formatação para números inteiros
# ││└─── "5": largura do resultado
# │└──── ">": alinhamento a direita
# └───── " ": preenchimento com espaços
Examples:
item = 123
print(f"{item: >5d}") # " 123"
print(f"{item: <5d}") # "123 "
print(f"{item: ^5d}") # " 123 "
print(f"{item:_>5d}") # "__123"
print(f"{item:0>7d}") # "0000123"
Behold Pyformat.info and the documentation for more information.
That said, now you should understand what my code does, basically:
Creates the string that will be used as a separator between PA terms.
If itens_por_linha
for 4
, creates an infinite iterator that generates 3 empty strings and 1 line break (['', '', '', '\n', '', '', '', '\n', '', ...]
).
These characters are used by print
to define whether the algorithm will break the line, or printara the term on the same line.
Traverse PA and limiter generator "parallel" using zip
Printa the result
Welcome. Next blocks of code, enter
para abrir e
to close the code block– Rogério Dec
This might help you: https://answall.com/questions/359166/formationdata-out
– G. Bittencourt