From what I understand, you want to implement a code that is able to display the N first numbers pairs in reverse order.
Another way to resolve this issue is:
n = int(input('Digite um número: '))
i = 0
pares = list()
while len(pares) <= n - 1:
if i % 2 == 0:
pares.append(i)
i += 1
print(sorted(pares, reverse=True))
Note that this code is able to display all numbers pairs amid 0 and N - No matter who this N is.
Testing the code:
When executing the code, enter the value 8 and press enter
, we receive as a result:
[14, 12, 10, 8, 6, 4, 2, 0]
Another way to resolve this issue is by using the loop for
. The code would stand:
n = int(input('Digite um número: '))
pares = list()
for i in range(0, (2 * n)):
if i % 2 == 0:
pares.append(i)
print(sorted(pares, reverse=True))
Another way to solve is by using List Comprehension. In this case the code would be:
pares = [i for i in range(0, (2 * int(input('Digite um número: ')))) if i % 2 == 0]
print(sorted(pares, reverse=True))
If you don’t want to build a list, then display it, you can use the code:
n = int(input('Digite um número: '))
m = (2 * (n - 1))
while m >= 0:
if m % 2 == 0:
print(m)
m -= 1
or
n = int(input('Digite um número: '))
for i in range((2 * (n - 1)), -1, -2):
if i % 2 == 0:
print(i, end=' ')
print()
Fix code formatting and indentation, please :)
– Vitor Ceolin
"if I inform 8, I have to print 0, 2, 4, 6, 8, 10, 12, 14", shouldn’t be in order decreasing?
– Woss
Yes, I was wrong to describe...
– EWSG
0, 2, 4, 6, 8..
is in ascending order. You either ascending or descending order?– hkotsubo
See help: https://answall.com/a/479716/137387
– Augusto Vasques