Call function list in python 3

-3

I am creating a script for my python course.

The script receives as input a sequence of integers terminated by zero and returns the numbers in ascending order, removing the repeated ones.

The script worked from the first compilation, but now that I have split it into a function it is giving some error with the 'list' I do not understand:

  

TypeError: remove_repetits () missing 1 required positional argument: 'list'

Follow my script:

def remove_repetidos (lista):
    lista2 = set(lista)
    lista = list(lista2)
    lista.sort()
    return lista


x = 1
lista = []
while x > 0:
    x  = int(input("Digite um número inteiro: "))
    lista.append(x)
del lista[-1]

remove_repetidos()
    
asked by anonymous 29.06.2018 / 18:30

2 answers

2

First, Python is an interpreted language not compiled, if you want to know more click here!

/ a>

Since your remove_repeat (list) function statement asks for a parameter you should pass it. in the case of lists, python does not create a copy of the list and instead passes a pointer from it.

Try this.

def remove_repetidos (lista):
    lista2 = set(lista)
    lista = list(lista2)
    lista.sort()
    return lista

def main():
   x = 1
   lista = []
   while x > 0:
       x  = int(input("Digite um número inteiro: "))
       lista.append(x)
   del lista[-1]

   remove_repetidos(lista)

main()
    
29.06.2018 / 18:49
0

The error says that you called the function remove_repetidos , which expects an argument, with no arguments. To fix this, just inform it properly. However, the new list will be returned by the function, so you need to capture this return.

It would look like this:

def remove_repetidos (lista):
    lista2 = set(lista)
    lista = list(lista2)
    lista.sort()
    return lista


x = 1
lista = []
while x > 0:
    x  = int(input("Digite um número inteiro: "))
    lista.append(x)
del lista[-1]

lista = remove_repetidos(lista)
#  ^                       ^                       
#  |                       +--- Passa o parâmetro para a função
#  +--- Captura o retorno da função

print(lista)  # Exibe a lista final

Another way to read the list would be:

lista = []

while True:
    x = int(input('Numero: '))
    if x == 0:
        break
    lista.append(x)
What prevents you from initializing x equal to 1, eliminates the wrong x > 0 condition of the loop (since the input can be negative) and avoids having to delete the last element, 0, from the list, added improperly.

Test the -5 -6 -2 -2 -3 0 input with both solutions and compare the results.

    
29.06.2018 / 18:53