Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

I cannot figure out how to fix this problem, can someone help me ? Pirple homework python

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;

extra is if we can make the remaining values to a list called my leftovers

myUniqueList = []
myLeftovers = []

def addUniqueElement(b):
    if b not in myUniqueList:
        print(myUniqueList.append(b))
        return True
    else:
        myLeftovers.append(newElement)
        return False
    
print(addUniqueElement())
question from:https://stackoverflow.com/questions/65946889/i-cannot-figure-out-how-to-fix-this-problem-can-someone-help-me-pirple-homewo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Something to note is that your attempt was very good. It did everything right except for a few things:

You should print out the list if you want to see the final list eg.

print(myUniqueList)

Next, the function requires an argument, in this case, I'll use "cool" so now we have

addUniqueElement("cool")
print(myUniqueList)

In the end we get

myUniqueList = []
myLeftovers = []

def addUniqueElement(b):
    if b not in myUniqueList:
        print(myUniqueList.append(b))
    else:
        myLeftovers.append(newElement)
addUniqueElement("cool")
print(myUniqueList)
print(myLeftovers)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...