I am John Jackson. I am a Python Homework Help Expert at pythonhomeworkhelp.com. I hold a Ph.D. in Programming from Texas Tech University, United States. See my sample on introduction to python Dictionary before hiring. For more, Visit www.pythonhomeworkhelp.com or email support@pythonhomeworkhelp.com, you can also call +1 (315) 557-6473.
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