List without negative number

Asked

Viewed 195 times

5

The exercise is as follows: a program that reads the values and separates them into a list of pairs or odd numbers. The count only stops when a negative number comes (which should not be on the list). I’ve done the program and it works only I can’t get the negative number off the list. My code is like this:

numero = [[], []]  
valor = 1  
while valor >= 0:  
    valor = int(input('Número: '))  
    if valor % 2 == 0:  
        numero[0].append(valor)  
    else:  
        numero[1].append(valor)  
print(f"{numero[0]}\n"  
      f"{numero[1]}")

3 answers

6


No need to create the variable outside the while with an artificial value only to enter it. You can do so:

numero = [[], []]  
while True:
    valor = int(input('Número: '))  
    if valor < 0: # número negativo
        break # sai do hile
    if valor % 2 == 0:
        numero[0].append(valor)  
    else:  
        numero[1].append(valor)  

print(f"{numero[0]}\n{numero[1]}")

while True creates a loop infinity - in fact, it repeats itself until it is interrupted by a break. And the break, in this case, it is called if the value is negative, which is its output condition. Thus, it leaves the while before entering the negative number in one of the lists, which is what you need.


Since the program will only insert positive numbers and the rest of the division by 2 will be zero or 1, you could also use the rest as the index:

numero = [[], []]  
while True:
    valor = int(input('Número: '))  
    if valor < 0: # número negativo
        break # sai do hile
    numero[valor % 2].append(valor)  

To another answer also works, but there is a catch: the condition for the number to be valid and inserted in the lists (numero >= 0) repeats itself three times in the code. And if you need to change this condition (for example, if the program can now accept negative numbers, and the positive ones can only be less than 100, or any other criteria), you will have to do so in these 3 places. Already using the code above, the condition is in a single point and just change there if you need.

Although it is only an exercise, it is important to think about these aspects, because avoiding this type of repetition helps a lot in the maintenance of the code - this principle is known as DRY (Don’t Repeat Yourself).

3

Another way to achieve this is to add a check to if: ...and valor >= 0:

So it would prevent a value less than 0 entered.

Code:

numero = [[], []]  
valor = 1  
while valor >= 0:  
    valor = int(input('Número: '))  
    if valor % 2 == 0 and valor >= 0:  
        numero[0].append(valor)  
    elif valor % 2 != 0 and valor >= 0:  
        numero[1].append(valor)  
print(f"{numero[0]}\n"  
      f"{numero[1]}")
  • Got it, thanks. The problem was really simple.

1

If you are using the Python 3.8 or higher, you can use Assignment Expressions to assemble your code. This way the code would be:

numero = [[], []]
while (n := int(input('Valor: '))) > -1:
    if n % 2 == 0:
        numero[0].append(n)
    else:
        numero[1].append(n)

print(f'Números pares: {numero[0]}')
print(f'Números ímpares: {numero[1]}')

How this code works?

The block while will capture each entered value as long as they are positive values. Next the block if will assess whether the value entered is even. If so, the value will be stored in number[0] - even numbers - and otherwise the value will be stored in number1 - odd numbers.

If any value is entered less than zero - negative value - the while block terminates its execution disregarding the last entered value.

Then the two lists will be displayed. One containing even values and the other containing odd numbers.

Browser other questions tagged

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