-
-
Save qmmr/ca6896428b4f9378d738ea8933fc048b to your computer and use it in GitHub Desktop.
| """ | |
| 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 = [] | |
| myLeftovers = [] | |
| def addToList(thing): | |
| if thing in myUniqueList: | |
| addToLeftovers(thing) | |
| return False | |
| else: | |
| myUniqueList.append(thing) | |
| return True | |
| def addToLeftovers(thing): | |
| myLeftovers.append(thing) | |
| # Test the addToList function | |
| print(myUniqueList) # [] | |
| print(addToList("dog")) # Returns True | |
| print(myUniqueList) # ['dog'] | |
| print(myLeftovers) # [] | |
| # Adding the element that already exists | |
| print(addToList("dog")) # Returns False | |
| print(myUniqueList) # ['dog'] | |
| print(myLeftovers) # ['dog'] | |
| # Adding a new element | |
| print(addToList("cat")) # Returns True | |
| print(myUniqueList) # ['dog', 'cat'] | |
| print(myLeftovers) # ['dog'] |
NadaBatt
commented
Dec 27, 2020
via email
Hello I need some help when I run this code
myUniqueList = []
myLeftovers = []
def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return True
print(myUniqueList) # []
print(addList("Meliodas")) # returns 'True' since it's a new item
print(addList("Escanor")) # returns 'True' since it's a new item
print(addList("Meliodas")) # returns 'False' since it's already been added
print(myUniqueList) # This includes the new entries
print(myLeftovers) # This includes any repeated entries
I get
[]
False
False
False
[]
[]
Can you explain why the list dont get the words added and why I get Syntax Error on the else statement
Hello I need some help when I run this code
myUniqueList = []
myLeftovers = []def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return Trueprint(myUniqueList) # []
print(addList("Meliodas")) # returns 'True' since it's a new item
print(addList("Escanor")) # returns 'True' since it's a new item
print(addList("Meliodas")) # returns 'False' since it's already been added
print(myUniqueList) # This includes the new entries
print(myLeftovers) # This includes any repeated entriesI get
[]
False
False
False
[]
[]Can you explain why the list dont get the words added and why I get Syntax Error on the else statement
Hello I need some help when I run this code
myUniqueList = []
myLeftovers = []def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return Trueprint(myUniqueList) # []
print(addList("Meliodas")) # returns 'True' since it's a new item
print(addList("Escanor")) # returns 'True' since it's a new item
print(addList("Meliodas")) # returns 'False' since it's already been added
print(myUniqueList) # This includes the new entries
print(myLeftovers) # This includes any repeated entriesI get
[]
False
False
False
[]
[]Can you explain why the list dont get the words added and why I get Syntax Error on the else statement
Hey, i've just had a look at your code, and it worked fine. I got the below output:
[]
True
True
False
['Meliodas', 'Escanor']
['Meliodas']
When i copied your code and ran it, i got a few indentation errors, so i tidied it up a bit, i dont know if this helps.
myUniqueList = []
myLeftovers = []
def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return True
print(myUniqueList) # []
print(addList("Meliodas")) # returns 'True' since it's a new item
print(addList("Escanor")) # returns 'True' since it's a new item
print(addList("Meliodas")) # returns 'False' since it's already been added
print(myUniqueList) # This includes the new entries
print(myLeftovers) # This includes any repeated entries
Hello I need some help when I run this code
myUniqueList = []
myLeftovers = []def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return Trueprint(myUniqueList) # []
print(addList("Meliodas")) # returns 'True' since it's a new item
print(addList("Escanor")) # returns 'True' since it's a new item
print(addList("Meliodas")) # returns 'False' since it's already been added
print(myUniqueList) # This includes the new entries
print(myLeftovers) # This includes any repeated entriesI get
[]
False
False
False
[]
[]Can you explain why the list dont get the words added and why I get Syntax Error on the else statement
myUniqueList = []
myLeftovers = []
def addToList(thing):
if thing in myUniqueList:
addToLeftovers(thing)
return False
else:
myUniqueList.append(thing)
return True
def addToLeftovers(thing):
myLeftovers.append(thing)
#Test the addToList function
print(myUniqueList) # []
print(addToList("dog")) # Returns True
print(myUniqueList) # ['dog']
print(myLeftovers) # []
#Adding the element that already exists
print(addToList("dog")) # Returns False
print(myUniqueList) # ['dog']
print(myLeftovers) # ['dog']
#Adding a new element
print(addToList("lion")) # Returns True
print(myUniqueList) # ['dog','lion']
print(myLeftovers) # ['dog']
Thank you, I don't know the word "in" so I can't do the homework