Last active
March 28, 2021 11:15
-
-
Save UcheAnyiam/c8ab2465e0e727967d02eb22b0b4d40b to your computer and use it in GitHub Desktop.
Homework Assignment #4: Lists Details: 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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
pirple.com/python | |
Homework Assignment #4: Lists | |
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. | |
Extra Credit: | |
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. | |
""" | |
myUniqueList = [] | |
rejectedinputs = [] | |
def addtolist(item): | |
if item in myUniqueList: | |
rejectedinputs.append(item) | |
return False | |
else: | |
myUniqueList.append(item) | |
return True | |
# adding "yes" to myUniqueList | |
print(addtolist("yes")) | |
print(myUniqueList) | |
print(rejectedinputs) | |
# adding "yes" again to myUniqueList and my rejectedinputs so that it prints False and is added into the rejected variable | |
print(addtolist("yes")) | |
print(myUniqueList) | |
print(rejectedinputs) | |
# | |
# # adding a different element to myUniqueList and rejectedinputs so it prints True and only gets added to myUniqueList | |
print(addtolist(66)) | |
print (myUniqueList) | |
print(rejectedinputs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment