SlideShare a Scribd company logo
1 of 1
Download to read offline
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
Solution
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

More Related Content

Similar to Write a function called countElements that counts the number of times.pdf

Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
MCCMOTOR
 

Similar to Write a function called countElements that counts the number of times.pdf (20)

Python Data Types (1).pdf
Python Data Types (1).pdfPython Data Types (1).pdf
Python Data Types (1).pdf
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptx
 
15CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 415CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 4
 
15CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 315CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 3
 
The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202
 
The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184
 
Python lists
Python listsPython lists
Python lists
 
Assume a list has the following element- write a function to interch.docx
Assume a list has the following element-   write a function to interch.docxAssume a list has the following element-   write a function to interch.docx
Assume a list has the following element- write a function to interch.docx
 
Given a list of numbers- count and return how many pairs there are- A.docx
Given a list of numbers- count and return how many pairs there are- A.docxGiven a list of numbers- count and return how many pairs there are- A.docx
Given a list of numbers- count and return how many pairs there are- A.docx
 
updated_list.pptx
updated_list.pptxupdated_list.pptx
updated_list.pptx
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
 
The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210
 
Python data type
Python data typePython data type
Python data type
 
Python PCEP Lists Collections of Data
Python PCEP Lists Collections of DataPython PCEP Lists Collections of Data
Python PCEP Lists Collections of Data
 
Python PRACTICAL NO 6 for your Assignment.pptx
Python PRACTICAL NO 6 for your Assignment.pptxPython PRACTICAL NO 6 for your Assignment.pptx
Python PRACTICAL NO 6 for your Assignment.pptx
 
The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189
 
Python - Data Collection
Python - Data CollectionPython - Data Collection
Python - Data Collection
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptx
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
 

More from feetshoemart

Convert the following expressions from infix to Reverse Polish Notati.pdf
Convert the following expressions from infix to Reverse Polish Notati.pdfConvert the following expressions from infix to Reverse Polish Notati.pdf
Convert the following expressions from infix to Reverse Polish Notati.pdf
feetshoemart
 
Computer Forensics MethodologiesList them and explain each one.P.pdf
Computer Forensics MethodologiesList them and explain each one.P.pdfComputer Forensics MethodologiesList them and explain each one.P.pdf
Computer Forensics MethodologiesList them and explain each one.P.pdf
feetshoemart
 
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdf
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdfDiscussion 1 Choose one of the Opinion Poll questions (click here f.pdf
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdf
feetshoemart
 
wk3 reply to prof.I need help with this question below a 200 words.pdf
wk3 reply to prof.I need help with this question below a 200 words.pdfwk3 reply to prof.I need help with this question below a 200 words.pdf
wk3 reply to prof.I need help with this question below a 200 words.pdf
feetshoemart
 
This is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdfThis is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdf
feetshoemart
 
The process of Imperialism and Colonialism played a role in the esta.pdf
The process of Imperialism and Colonialism played a role in the esta.pdfThe process of Imperialism and Colonialism played a role in the esta.pdf
The process of Imperialism and Colonialism played a role in the esta.pdf
feetshoemart
 
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdfTerrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
feetshoemart
 
What do viral genomes look like compared to those of living organism.pdf
What do viral genomes look like compared to those of living organism.pdfWhat do viral genomes look like compared to those of living organism.pdf
What do viral genomes look like compared to those of living organism.pdf
feetshoemart
 
Using following main file and solve the taskInst.pdf
Using following main file and solve the taskInst.pdfUsing following main file and solve the taskInst.pdf
Using following main file and solve the taskInst.pdf
feetshoemart
 
11. Define a simple deformable model to detect a half-circular shape.pdf
11. Define a simple deformable model to detect a half-circular shape.pdf11. Define a simple deformable model to detect a half-circular shape.pdf
11. Define a simple deformable model to detect a half-circular shape.pdf
feetshoemart
 

More from feetshoemart (20)

Convert the following expressions from infix to Reverse Polish Notati.pdf
Convert the following expressions from infix to Reverse Polish Notati.pdfConvert the following expressions from infix to Reverse Polish Notati.pdf
Convert the following expressions from infix to Reverse Polish Notati.pdf
 
Andy and Joan are married and under 65 years of age. They have two c.pdf
Andy and Joan are married and under 65 years of age. They have two c.pdfAndy and Joan are married and under 65 years of age. They have two c.pdf
Andy and Joan are married and under 65 years of age. They have two c.pdf
 
Compare the methods by which the Parsees of India benefitted from th.pdf
Compare the methods by which the Parsees of India benefitted from th.pdfCompare the methods by which the Parsees of India benefitted from th.pdf
Compare the methods by which the Parsees of India benefitted from th.pdf
 
Computer Forensics MethodologiesList them and explain each one.P.pdf
Computer Forensics MethodologiesList them and explain each one.P.pdfComputer Forensics MethodologiesList them and explain each one.P.pdf
Computer Forensics MethodologiesList them and explain each one.P.pdf
 
Document2- Word (Product Activation Failed) Mailings Review View Tell.pdf
Document2- Word (Product Activation Failed) Mailings Review View Tell.pdfDocument2- Word (Product Activation Failed) Mailings Review View Tell.pdf
Document2- Word (Product Activation Failed) Mailings Review View Tell.pdf
 
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdf
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdfDiscussion 1 Choose one of the Opinion Poll questions (click here f.pdf
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdf
 
calculate the hydrogen ion concentration in mol for the following s.pdf
calculate the hydrogen ion concentration in mol for the following s.pdfcalculate the hydrogen ion concentration in mol for the following s.pdf
calculate the hydrogen ion concentration in mol for the following s.pdf
 
At a certain temperature, 0.5011 mol of N2 and 1.781 mol of H2 are pl.pdf
At a certain temperature, 0.5011 mol of N2 and 1.781 mol of H2 are pl.pdfAt a certain temperature, 0.5011 mol of N2 and 1.781 mol of H2 are pl.pdf
At a certain temperature, 0.5011 mol of N2 and 1.781 mol of H2 are pl.pdf
 
Consider mappinp, phi Z rightarrow R, defined as phi(x) = 2x, where .pdf
Consider mappinp, phi Z rightarrow R, defined as phi(x) = 2x, where .pdfConsider mappinp, phi Z rightarrow R, defined as phi(x) = 2x, where .pdf
Consider mappinp, phi Z rightarrow R, defined as phi(x) = 2x, where .pdf
 
wk3 reply to prof.I need help with this question below a 200 words.pdf
wk3 reply to prof.I need help with this question below a 200 words.pdfwk3 reply to prof.I need help with this question below a 200 words.pdf
wk3 reply to prof.I need help with this question below a 200 words.pdf
 
Write a program that prompts the user to enter a positive integer and.pdf
Write a program that prompts the user to enter a positive integer and.pdfWrite a program that prompts the user to enter a positive integer and.pdf
Write a program that prompts the user to enter a positive integer and.pdf
 
This is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdfThis is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdf
 
There is no video, All you have to do is show how you reference the .pdf
There is no video, All you have to do is show how you reference the .pdfThere is no video, All you have to do is show how you reference the .pdf
There is no video, All you have to do is show how you reference the .pdf
 
The process of Imperialism and Colonialism played a role in the esta.pdf
The process of Imperialism and Colonialism played a role in the esta.pdfThe process of Imperialism and Colonialism played a role in the esta.pdf
The process of Imperialism and Colonialism played a role in the esta.pdf
 
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdfTerrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
 
What do viral genomes look like compared to those of living organism.pdf
What do viral genomes look like compared to those of living organism.pdfWhat do viral genomes look like compared to those of living organism.pdf
What do viral genomes look like compared to those of living organism.pdf
 
Verify the identity Verify the identity by transforming the left-han.pdf
Verify the identity Verify the identity by transforming the left-han.pdfVerify the identity Verify the identity by transforming the left-han.pdf
Verify the identity Verify the identity by transforming the left-han.pdf
 
Using following main file and solve the taskInst.pdf
Using following main file and solve the taskInst.pdfUsing following main file and solve the taskInst.pdf
Using following main file and solve the taskInst.pdf
 
Suppose there are 14 children trying to form two teams with seven ch.pdf
Suppose there are 14 children trying to form two teams with seven ch.pdfSuppose there are 14 children trying to form two teams with seven ch.pdf
Suppose there are 14 children trying to form two teams with seven ch.pdf
 
11. Define a simple deformable model to detect a half-circular shape.pdf
11. Define a simple deformable model to detect a half-circular shape.pdf11. Define a simple deformable model to detect a half-circular shape.pdf
11. Define a simple deformable model to detect a half-circular shape.pdf
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Recently uploaded (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

Write a function called countElements that counts the number of times.pdf

  • 1. 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 Solution 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