Python Range - Numbers in descending order

Asked

Viewed 1,399 times

-3

I need to generate a list with even numbers of N until 0.

For example, if I inform 8, I have to print 14, 12, 10, 8, 6, 4, 2, 0. My program, below, is returning 9 numbers, going up to the 16. Where did I go wrong, please? :)

numero = int(input('Informe um número inteiro positivo e par: '))
while numero <= 0 or numero % 2 != 0:
    print('O número não pode ser zero, ímpar ou negativo!')
    numero = int(input('Informe um número inteiro positivo e par: '))
while numero > 0 or numero % 2 == 0:
    for n in range(numero + numero, - 1, - 2):
        print(n)
    break
  • Fix code formatting and indentation, please :)

  • 1

    "if I inform 8, I have to print 0, 2, 4, 6, 8, 10, 12, 14", shouldn’t be in order decreasing?

  • Yes, I was wrong to describe...

  • 0, 2, 4, 6, 8.. is in ascending order. You either ascending or descending order?

  • See help: https://answall.com/a/479716/137387

3 answers

1

A very easy way is to use the Python range function Try this function:

def f(n):
for i in range((n-1)*2, -1, -2):
    print(i)

When using the function range(start, end, step), if you use a negative step, you will have the numbers in descending order Here you can find more details and some examples of the use of the range function: https://www.w3schools.com/python/ref_func_range.asp

  • Hello Rafael! Thank you very much! :)

  • Now, for example, in the algorithm below, for Brazilian numbers, I’m not getting it, either.

  • numero = int(input('Informe um número inteiro positivo e ímpar: '))&#xA;while numero <= 0 or numero % 2 == 0:&#xA; print('O número não pode ser zero, par ou negativo!')&#xA; numero = int(input('Informe um número inteiro positivo e ímpar: '))&#xA;while numero > 0 or numero % 2 != 0:&#xA; for n in range(numero, 0, - 2):&#xA; print(n)&#xA; break

  • I wanted to inform, above, 5, and the program return 5 numbers in descending order, all odd. Sorry for the formatting problems, guys. ( I’m not getting through.

0

The way you built the algorithm you need to get 2 from the start of the range(start, stop, step):

numero = int(input('Informe um número inteiro positivo e par: '))
while numero <= 0 or numero % 2 != 0:
    print('O número não pode ser zero, ímpar ou negativo!')
    numero = int(input('Informe um número inteiro positivo e par: '))
else:
    for n in range((numero + numero) - 2, - 1, - 2):
        print(n)

Entree:

Informe um número inteiro positivo e par: 8

Exit:

14
12
10
8
6
4
2
0
  • 1

    I get it. Thank you. :)

0

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()
  • It’s just that I haven’t gotten to that part yet in the course.

  • @EWSG, I’ve cited three ways to solve it. The first with a bow while, the second with bow for and the third with List Comprehension. You can use one of the three. They will all give you the desired result.

  • @EWSG, if you type 8, you will receive as a result a list with 8 números pares incluindo 0. If you type 5, you will receive as a result a list with 5 números pares and so on.

  • 1

    You gave it right. Thank you, buddy. :)

Browser other questions tagged

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