Lists | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Lists

myUniqueList = [] def add(x): myUniqueList.append(x) add(10) print(myUniqueList) Can anyone helo me create the code i dont understand English that good and the code above is how far i got and i dont know how to write the rest that it asks (i am learning the python from an website and the gave me this homework) Create a global variable called myUniqueList. It should be an empty list to start. Next, create a function that allows you to add things to that list. Anything that's passed to this function should get added to myUniqueList, unless its value already exists in myUniqueList. If the value doesn't exist already, it should be added and the function should return True. If the value does exist, it should not be added, and the function should return False; Finally, add some code below your function that tests it out. It should add a few different elements, showcasing the different scenarios, and then finally it should print the value of myUniqueList to show that it worked.

8th Feb 2020, 2:05 PM
kasisX
kasisX - avatar
15 Answers
+ 1
Okay, you're off to a good start. However, a couple of things need to be added. Because myUniqueList is a global list, if we want to use it in a function we need to inform the function we're using it by doing this: global myUniqueList You can place this inside your function. Next, we also need to check if the parameter provided to the function is already in myUniqueList. We can do this by using in: if x in myUniqueList: #if x is already in myUniqueList return False #the challenge asks you to return False if x is already in myUniqueList else: myUniqueList.append(x) #if it's not, we can append it return True #and return True, which the challenge tells us to do Try this out, and add a couple of examples to see if it works.
8th Feb 2020, 2:14 PM
Jianmin Chen
Jianmin Chen - avatar
0
All it says is that if you have entered 10 once in the list then if you try to add the same value again to the list then it should not be allowed. You just have to avoid duplicates, be it any type you are passing in the method.
8th Feb 2020, 2:14 PM
Avinesh
Avinesh - avatar
0
Ok ty
10th Feb 2020, 6:20 AM
kasisX
kasisX - avatar
0
myUniqueList = [] def add(x): if x in myUniqueList: return False else: myUniqueList.append(x) return True add(9) print(myUniqueList) How do i make it so it remembers whats already in list
10th Feb 2020, 1:42 PM
kasisX
kasisX - avatar
0
You need to use the global variable, after defining add(): def add(x): global myUniqueList #rest of your code goes here
10th Feb 2020, 1:45 PM
Jianmin Chen
Jianmin Chen - avatar
0
Def add(×): Global myUniqueList = [] Or i just add the variable above the func and do it like you said: MyUniqueList = [] Def add(x): Global myUniqueList ... Like this ?
10th Feb 2020, 1:49 PM
kasisX
kasisX - avatar
0
The bottom one is correct(global and def should be all lowercase though).
10th Feb 2020, 1:50 PM
Jianmin Chen
Jianmin Chen - avatar
0
Ok ty
10th Feb 2020, 1:51 PM
kasisX
kasisX - avatar
0
myUniqueList = [] def add(x): global myUniqueList if x in myUniqueList: return False else: myUniqueList.append(x) return True add(10) print(myUniqueList) But how is the if x in myUniquelist part is supposed to work it doesnt return me false if i keep on hitting run with the same number
10th Feb 2020, 1:54 PM
kasisX
kasisX - avatar
0
It works. If you want to see whether the function returns True or False, you would have to use a print() statement, like this: print(add(10)) Or: ten = add(10) #basically, a value is given to the function, which is exchanged to the variable print(ten) So, if you were to run this: print(add(10)) print(add(20)) print(add(10)) print(myUniqueList) The result in the console would be: True True False [10, 20]
10th Feb 2020, 5:14 PM
Jianmin Chen
Jianmin Chen - avatar
0
I tryed to run it it all works but when i keep adding the same number ir returns me the same number and true and when i run it with different number they dont stack up in the list it just shows a different number and true myUniqueList = [] def add(x): global myUniqueList if x in myUniqueList return False else: myUniqueList.append(x) return true print(add(10)) print (myUniqueList) #running the code two times output: True [10] True [10]
10th Feb 2020, 5:36 PM
kasisX
kasisX - avatar
0
It works fine for me.
10th Feb 2020, 7:39 PM
Jianmin Chen
Jianmin Chen - avatar
0
@kasisX actually in your code there is indentation mistake in else section and you cannot use colon : after if, you write true in the small letter but it is the not right way writing true, in boolean you write true like this:- True and you do not call add function twice with the same value for checking, when you call add function twice with same value then it gives first time True and other time False. Try this code I solved all your mistake in this code:- myUniqueList = [] def add(x): global myUniqueList if x in myUniqueList: return False else: myUniqueList.append(x) return True print(add(10)) print (myUniqueList) print(add(10)) print(myUniqueList) #running the code two times #output: # True # [10] # True # [10]
15th Apr 2020, 11:47 AM
AMIT DHUSSA
0
Man this was 2 months ago but thanks anyways now i understand i had to do double print for func and list ty
15th Apr 2020, 11:53 AM
kasisX
kasisX - avatar
0
Add another function that pushes all the rejected inputs into a separate global array called myLeftovers. If someone tries to add a value to myUniqueList but it's rejected (for non-uniqueness), it should get added to myLeftovers instead.?/??
16th Nov 2020, 6:46 AM
Arman
Arman - avatar