0

This code has 2 functions one for taking a list from user and the other one should remove the duplications in this list , it can be done without using functions but im learning functions in Python. How can I take the list that the user has entered in function take_list() and pass it to the function no_duplication() ? when I do it i get this error

Unresolved reference 'k'

at the line : no_duplication(k)

def take_list():
    k = []
    i = 0
    list_length = int(input("how many elements your list has ? \n "))
    while i < list_length:
        element = int(input("enter you list elements : "))
        k.append(element)
        i += 1
    print(k, "  is your list")
    return k

def no_duplication(k):
    k = set(k)
    k = list(k)
    print(k, " Your list without duplications")
 

take_list()
no_duplication(k)
3
  • 2
    k is undefined in the last line of your code. You want k=take_list() and then no_duplication(k). Or, taken together, you can do: no_duplication(take_list()). Commented Jan 17, 2021 at 14:24
  • @trincot Thank you ! I didnt know there is such a way . Commented Jan 17, 2021 at 14:25
  • 1
    @MrXQ, just to avoid confusion - k in k = take_list() is different variable from k used inside the function body. One can use different name if they want. Commented Jan 17, 2021 at 14:27

1 Answer 1

1

You need to assign returned value to a new variable and pass that to no_duplication as parameter:

k = take_list()
no_duplication(k)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.