SlideShare a Scribd company logo
1 of 8
+1 (315)557-6473
https://www.pythonhomeworkhelp.com/
support@pythonhomeworkhelp.com
Kolb Rd. , Tucson, Arizona,
USA
Problem 1 – Names and Ages
Define two lists at the top of your file:
NAMES = [“Alice”, “Bob”, “Cathy”, “Dan”, “Ed”, “Frank”,
“Gary”, “Helen”, “Irene”, “Jack”, “Kelly”, “Larry”]
AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]
These lists match up, so Alice’s age is 20, Bob’s age is 21, and so on.
Write a program that combines these lists into a dictionary. Then, write a function that, given an age,
returns the names of all the people who are that age.
Test your program and function by running these lines: (replace people with your function)
print people(18) == [“Cathy”, “Dan”]
print people(19) == [“Ed”, “Helen”, “Irene”, “Jack”, “Larry”]
print people(20) == [“Alice”, “Frank”, “Gary”]
print people(21) == [“Bob”]
print people(22) == [“Kelly”]
print people(23) == []
All lines should print True. If the last line is giving you an error, look at the name of the error.
Solution:
NAMES = ["Alice", "Bob", "Cathy", "Dan", "Ed", "Frank", "Gary", "Helen", "Irene", "Jack", "Kelly",
"Larry"]
AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]
# Put code here that will combine these lists into a dictionary ages_to_names = {}
for i in range(len(NAMES)):
name = NAMES[i]
age = AGES[i]
if age in ages_to_names:
ages_to_names[age].append(name)
else:
ages_to_names[age] = [name] # the LIST containing the name
# You can rename this function
def people(age):
""" Return the names of all the people who are the given age. """
if age in ages_to_names:
return ages_to_names[age]
return []
# Testing
print people(18) == ["Cathy", "Dan"]
print people(19) == ["Ed", "Helen", "Irene", "Jack", "Larry"]
print people(20) == ["Alice", "Frank", "Gary"]
print people(21) == ["Bob"]
print people(22) == ["Kelly"]
print people(23) == []
Problem 2 -
The inventory shows eight different items, each having a name, a price and a count, like so:
HAMMER = “hammer”
HAMMER_PRICE = 10
HAMMER_COUNT = 100
We’re going to consider that customers generally come in with an idea of how much money they want
to spend. So we’re going to think of items as either CHEAP (under $20), MODERATE (between $20 and
$100) or EXPENSIVE (over $100).
First, fill in the variable inventory so that all of the data for the eight items is inside inventory. Make sure
that you maintain the notion of CHEAP, MODERATE and EXPENSIVE. Then, implement the function
get_info that takes a cheapness and returns a list of information about each item that falls under that
category, as the function’s information says.
When you’re finished, just run the program. All of the testing lines should print True.
Solution:
CHEAP = "cheap" # less than $20
MODERATE = "moderate" # between $20 and $100
EXPENSIVE = "expensive" # more than $100
HAMMER = "hammer"
HAMMER_PRICE = 10
HAMMER_COUNT = 100
SCREW = "screw"
SCREW_PRICE = 1
SCREW_COUNT = 1000
NAIL = "nail"
NAIL_PRICE = 1
NAIL_COUNT = 1000
SCREWDRIVER = "screwdriver"
SCREWDRIVER_PRICE = 8
SCREWDRIVER_COUNT = 100
DRILL = "drill"
DRILL_PRICE = 50
DRILL_COUNT = 20
WORKBENCH = "workbench"
WORKBENCH_PRICE = 150
WORKBENCH_COUNT = 5
HANDSAW = "handsaw"
HANDSAW_PRICE = 15
HANDSAW_COUNT = 50
CHAINSAW = "chainsaw"
CHAINSAW_PRICE = 80
CHAINSAW_COUNT = 30
# You should put the stuff logically into this dictionary.
# You can just put it all in right here, like shown.
# Try to use only one *variable*, called inventory here.
inventory = {
CHEAP : {
HAMMER : (HAMMER_PRICE, HAMMER_COUNT),
NAIL : (NAIL_PRICE, NAIL_COUNT),
SCREW : (SCREW_PRICE, SCREW_COUNT),
SCREWDRIVER : (SCREWDRIVER_PRICE, SCREWDRIVER_COUNT),
HANDSAW : (HANDSAW_PRICE, HANDSAW_COUNT)
},
MODERATE : {
DRILL : (DRILL_PRICE, DRILL_COUNT),
CHAINSAW : (CHAINSAW_PRICE, CHAINSAW_COUNT)
},
EXPENSIVE : {
WORKBENCH : (WORKBENCH_PRICE, WORKBENCH_COUNT)
}
}
def get_items(cheapness):
""" Return a list of (item, (price, count)) tuples that are the given cheapness. Note that the second
element of the tuple is another tuple. """
return inventory[cheapness].items()
# Testing
cheap = get_items(CHEAP)
print type(cheap) is list
print len(cheap) == 5
print (HAMMER, (HAMMER_PRICE, HAMMER_COUNT)) in cheap
print (NAIL, (NAIL_PRICE, NAIL_COUNT)) in cheap
print (SCREW, (SCREW_PRICE, SCREW_COUNT)) in cheap
print (SCREWDRIVER, (SCREWDRIVER_PRICE, SCREWDRIVER_COUNT)) in cheap
print (HANDSAW, (HANDSAW_PRICE, HANDSAW_COUNT)) in cheap
moderate = get_items(MODERATE)
print type(moderate) is list
print len(moderate) == 2
print (DRILL, (DRILL_PRICE, DRILL_COUNT)) in moderate
print (CHAINSAW, (CHAINSAW_PRICE, CHAINSAW_COUNT)) in moderate
expensive = get_items(EXPENSIVE)
print type(expensive) is list
print len(expensive) == 1
print (WORKBENCH, (WORKBENCH_PRICE, WORKBENCH_COUNT)) in expensive

More Related Content

More from Python Homework Help

More from Python Homework Help (18)

Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Programming Homework Help.pptx
Python Programming Homework Help.pptx
 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
 
Perfect Python Homework Help
Perfect Python Homework HelpPerfect Python Homework Help
Perfect Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Programming Homework Help
Python Programming Homework HelpPython Programming Homework Help
Python Programming Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 

Recently uploaded

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
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 ModeThiyagu K
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
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
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 

Introduction to Python Dictionary.pptx

  • 2. Problem 1 – Names and Ages Define two lists at the top of your file: NAMES = [“Alice”, “Bob”, “Cathy”, “Dan”, “Ed”, “Frank”, “Gary”, “Helen”, “Irene”, “Jack”, “Kelly”, “Larry”] AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19] These lists match up, so Alice’s age is 20, Bob’s age is 21, and so on. Write a program that combines these lists into a dictionary. Then, write a function that, given an age, returns the names of all the people who are that age. Test your program and function by running these lines: (replace people with your function) print people(18) == [“Cathy”, “Dan”] print people(19) == [“Ed”, “Helen”, “Irene”, “Jack”, “Larry”] print people(20) == [“Alice”, “Frank”, “Gary”] print people(21) == [“Bob”] print people(22) == [“Kelly”] print people(23) == [] All lines should print True. If the last line is giving you an error, look at the name of the error.
  • 3. Solution: NAMES = ["Alice", "Bob", "Cathy", "Dan", "Ed", "Frank", "Gary", "Helen", "Irene", "Jack", "Kelly", "Larry"] AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19] # Put code here that will combine these lists into a dictionary ages_to_names = {} for i in range(len(NAMES)): name = NAMES[i] age = AGES[i] if age in ages_to_names: ages_to_names[age].append(name) else: ages_to_names[age] = [name] # the LIST containing the name # You can rename this function def people(age): """ Return the names of all the people who are the given age. """ if age in ages_to_names: return ages_to_names[age] return []
  • 4. # Testing print people(18) == ["Cathy", "Dan"] print people(19) == ["Ed", "Helen", "Irene", "Jack", "Larry"] print people(20) == ["Alice", "Frank", "Gary"] print people(21) == ["Bob"] print people(22) == ["Kelly"] print people(23) == [] Problem 2 - The inventory shows eight different items, each having a name, a price and a count, like so: HAMMER = “hammer” HAMMER_PRICE = 10 HAMMER_COUNT = 100 We’re going to consider that customers generally come in with an idea of how much money they want to spend. So we’re going to think of items as either CHEAP (under $20), MODERATE (between $20 and $100) or EXPENSIVE (over $100). First, fill in the variable inventory so that all of the data for the eight items is inside inventory. Make sure that you maintain the notion of CHEAP, MODERATE and EXPENSIVE. Then, implement the function get_info that takes a cheapness and returns a list of information about each item that falls under that category, as the function’s information says.
  • 5. When you’re finished, just run the program. All of the testing lines should print True. Solution: CHEAP = "cheap" # less than $20 MODERATE = "moderate" # between $20 and $100 EXPENSIVE = "expensive" # more than $100 HAMMER = "hammer" HAMMER_PRICE = 10 HAMMER_COUNT = 100 SCREW = "screw" SCREW_PRICE = 1 SCREW_COUNT = 1000 NAIL = "nail" NAIL_PRICE = 1 NAIL_COUNT = 1000 SCREWDRIVER = "screwdriver" SCREWDRIVER_PRICE = 8 SCREWDRIVER_COUNT = 100
  • 6. DRILL = "drill" DRILL_PRICE = 50 DRILL_COUNT = 20 WORKBENCH = "workbench" WORKBENCH_PRICE = 150 WORKBENCH_COUNT = 5 HANDSAW = "handsaw" HANDSAW_PRICE = 15 HANDSAW_COUNT = 50 CHAINSAW = "chainsaw" CHAINSAW_PRICE = 80 CHAINSAW_COUNT = 30 # You should put the stuff logically into this dictionary. # You can just put it all in right here, like shown. # Try to use only one *variable*, called inventory here. inventory = { CHEAP : { HAMMER : (HAMMER_PRICE, HAMMER_COUNT), NAIL : (NAIL_PRICE, NAIL_COUNT),
  • 7. SCREW : (SCREW_PRICE, SCREW_COUNT), SCREWDRIVER : (SCREWDRIVER_PRICE, SCREWDRIVER_COUNT), HANDSAW : (HANDSAW_PRICE, HANDSAW_COUNT) }, MODERATE : { DRILL : (DRILL_PRICE, DRILL_COUNT), CHAINSAW : (CHAINSAW_PRICE, CHAINSAW_COUNT) }, EXPENSIVE : { WORKBENCH : (WORKBENCH_PRICE, WORKBENCH_COUNT) } } def get_items(cheapness): """ Return a list of (item, (price, count)) tuples that are the given cheapness. Note that the second element of the tuple is another tuple. """ return inventory[cheapness].items() # Testing cheap = get_items(CHEAP) print type(cheap) is list print len(cheap) == 5 print (HAMMER, (HAMMER_PRICE, HAMMER_COUNT)) in cheap
  • 8. print (NAIL, (NAIL_PRICE, NAIL_COUNT)) in cheap print (SCREW, (SCREW_PRICE, SCREW_COUNT)) in cheap print (SCREWDRIVER, (SCREWDRIVER_PRICE, SCREWDRIVER_COUNT)) in cheap print (HANDSAW, (HANDSAW_PRICE, HANDSAW_COUNT)) in cheap moderate = get_items(MODERATE) print type(moderate) is list print len(moderate) == 2 print (DRILL, (DRILL_PRICE, DRILL_COUNT)) in moderate print (CHAINSAW, (CHAINSAW_PRICE, CHAINSAW_COUNT)) in moderate expensive = get_items(EXPENSIVE) print type(expensive) is list print len(expensive) == 1 print (WORKBENCH, (WORKBENCH_PRICE, WORKBENCH_COUNT)) in expensive