Python Fundamentals Practice Problem #7

Screen Link:

My Code:

Paste your code here

What I expected to happen:

What actually happened:

Paste output/error here

Hi all,

I’m on practice problem 7 for the python fundamentals course Pt 1 and the answer is below.

from csv import reader

read the CSV file

file = open(“laptops.csv”)
file_reader = reader(file)
rows = list(file_reader)[1:] # ignore column headers

create the dictionary mapping the prices to the laptop names

price_to_name = {}
for row in rows:
price = int(row[2])
name = row[1]
if price in price_to_name:
price_to_name[price] = [name]

initialize the two laptops in the answer to None

laptop1 = None
laptop2 = None

look for a solution

for row in rows:
price = int(row[2])
# this laptop’s cost is price, so if there is a laptop that
# costs 5000 - price then their total price is exactly 5000
if price == 2500 and len(price_to_name[2500]) >= 2:
laptop1 = price_to_name[2500][0]
laptop2 = price_to_name[2500][1]
elif 5000 - price in price_to_name:
laptop1 = price_to_name[price][0]
laptop2 = price_to_name[5000 - price][0]

print the solution


I am pretty confused on what is happening from the point where it says #look for a solution, and onwards. Is there anyone that can walk me through what is happening beyond that point?


hey @sbolourc

For a start check out this post. Let us know if this still doesn’t helps you much.