Call list in function in python 3

Asked

Viewed 415 times

-3

I am creating a script for my python course.

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

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

Typeerror: remove_repeated() 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()
  • Failed to pass the list as the remove_repeated parameter()

  • I guess I’m having a hard time on that question anyway, how do I do that?

  • just do remove_repetidos(lista) there on the last line

  • did this and gave another error now, remove_repeated() take 0 positional arguments, but 1 was Given

  • What mistake? Here when I copied and pasted gave error too. In my case it was only because of the accent in "number". After that, it worked normally as expected. See if suddenly that’s it too.

  • I decided to put list between the function parentheses there at the beginning of the code, and at the end also when calling the function, would that be it? Is there any way to put the parameter only in the function call? Example, if I was working with two lists and want to use the same function to organize them

Show 1 more comment

2 answers

2

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

As your statement of function remove_repeated (list) asks a parameter you must pass it. in the case of lists python does not create a copy of the list but passes a pointer of the list.

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()
  • 1

    In this case, the copy of the list is being made in the conversion to set and to list again, then lista that is returned will be different from the lista input, which requires you to use the function return.

0

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

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 having to initialize the x equal to 1, eliminates the condition wrong x > 0 loop (because the entry may be negative) and avoid having to delete the last element, 0, from the list, added improperly.

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

  • Thank you, it really got better this way, but unfortunately I got a zero score on my test, not what is happening because my program is working as expected. Here is the statement :Exercise 1 - Removing repeated elements Write the remove_repeated function you receive as a parameter a list of integer numbers, check that such list has repeated elements and remove them. The function should return a list corresponding to the first list, without repeating elements. The returned list should be sorted. Tip: You can use list.Sort() or Sorted(list). What’s the difference?

Browser other questions tagged

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