Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function called countElements that counts the number of times two elemen

ID: 3776895 • Letter: W

Question

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(countUst, element1, element2): Example: 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.py

Explanation / Answer

def countElements(list, ch1, ch2):
count = 0
for i in range(0,len(list)):
if list[i] == ch1:
count = count + 1
for i in range(0,len(list)):
if list[i] == ch2:
count = count + 1
return count;
  

print countElements(["t","s",7,7,2], "t", 7)

Output:

sh-4.3$ python main.py   

3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote