Posts: 69
Threads: 20
Joined: Sep 2019
Hi,
I was doing this exercise of practicePython site and I don't know why doesn't work
Write a program (function!) that takes a list and returns a new list that contains all the elements of the first list minus all the duplicates.
'''
#This create a list from a input user
#create a set from a list
def create_set(list_create):
user_input = input("Insert elements: ")
list_create = list(map(int,user_input.split(',')))
print(list_create)
return list(set(list_create)) Can you help me?
Regards,
RavCoder
Posts: 4,220
Threads: 97
Joined: Sep 2016
Define "doesn't work." I'm not sure what the parameter to the function call is supposed to be, as you write over whatever it is on line 10. But if you pass something as a parameter, it works for me. Note that it is printing out the list before dupes are removed, and returning the list with dupes removed. So it may look wrong, but if you check the return value it's correct.
Posts: 69
Threads: 20
Joined: Sep 2019
(Oct-23-2019, 01:42 PM)ichabod801 Wrote: Define "doesn't work." I'm not sure what the parameter to the function call is supposed to be, as you write over whatever it is on line 10. But if you pass something as a parameter, it works for me. Note that it is printing out the list before dupes are removed, and returning the list with dupes removed. So it may look wrong, but if you check the return value it's correct.
When I added this above function and compiled my code :
list_input = input()
print(create_set(list_input)) Didn't show me nothing as if it didn't read my script.
This is my all code:
def create_set(list_create):
user_input = input("Insert elements: ")
list_create = list(map(int,user_input.split(',')))
print(list_create)
return list(set(list_create))
list_input = input()
print(create_set(list_input))
Posts: 4,220
Threads: 97
Joined: Sep 2016
Again, that works for me. Note that the assignment you quoted says write a function that takes a list. Move the user_input= and list_create= lines outside the function, and then pass list_create to the function.
Posts: 69
Threads: 20
Joined: Sep 2019
Like this?
user_input = input("Insert elements: ")
list_create = list(map(int,user_input.split(',')))
def create_set(list_create):
print(list_create)
print (list(set(list_create)))
Posts: 1,358
Threads: 2
Joined: May 2019
You then need to call the create_set function in line 3. Otherwise the function is there but never executed.
Posts: 69
Threads: 20
Joined: Sep 2019
(Oct-23-2019, 02:58 PM)jefsummers Wrote: You then need to call the create_set function in line 3. Otherwise the function is there but never executed.
user_input = input("Insert elements: ")
list_create = list(map(int,user_input.split(',')))
print(create_set(list_create))
def create_set(list_create):
print(list_create)
print (list(set(list_create))) Give me an error:
Error: NameError: name 'create_set' is not defined
Posts: 1,358
Threads: 2
Joined: May 2019
Move lines 1,2,and 3 to 10, 11, and 12.
Posts: 69
Threads: 20
Joined: Sep 2019
(Oct-23-2019, 03:30 PM)jefsummers Wrote: Move lines 1,2,and 3 to 10, 11, and 12.
Thanks it works!!! but why if you put the function above doesn't recognize the call to the function?
Posts: 1,358
Threads: 2
Joined: May 2019
Getting into compiler theory a bit here. In an interpreted language like Python, the interpreter (what you probably think of as "Python" can do a pass through the code to pick up all the symbols before making a second pass through the code to execute, or it can do a single pass and require symbols to be declared before use. The latter is faster, and this is what Python appears to do. It's very consistent - location, indentation all matter very much in Python.
|