CODE IN PYTHONK PLEASE Write a function called countElements that counts the num
ID: 3776992 • Letter: C
Question
CODE IN PYTHONK PLEASE
Write a function called countElements that counts the number of times two elements appear in a list. Your task is to ask the user to enter a list and two elements which they need to count in the list. The list and each of the elements should be inputs to a function called countElements. The function should print the count of the number of times the elements appear in the list. def countElements(countList, element1, element2): if the inputs to the function were ["t", "s", 7, 7, 2], "t", and 7, the function would count the instances of the letter "t" and the number 7 in the list, and print the number 3. There was one "t" and two 7's. Name your file Lastname Firstname.pyExplanation / Answer
def countElements(l,c1,c2):
c = 0
f = 0
s = 0
for i in range(len(l)):
if(l[i]==c1):
c+=1
f+=1
elif(l[i]==c2):
c+=1
s+=1
print c,"Elements are present"
print "There was",f,'->',c1,"and",s,'->',c2
l = raw_input("Enter Elements of list(comma separated):").split(",")
c1 = raw_input("Enter first search element:")
c2 = raw_input("Enter second search element:")
countElements(l,c1,c2)
"""
sample output
Enter Elements of list(comma separated): t,s,7,7,2
Enter first search element: t
Enter second search element: 7
3 Elements are present
There was 1 -> t and 2 -> 7
"""
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.