1011/python-fundamentals-practice-problems/12/laptop-purchase

Hello all,
I have checked thru all other related Q&A but still do not understand below, hope you can assist me.

elif 5000 - price in price_to_name:
    laptop1 = price_to_name[price][0]
    laptop2 = price_to_name[5000 - price][0]

Refer to above solution, since we didn’t assign any variable to “price”. How would python understand what the “price” is and continue the rest of the instruction and find out the result.

Thanks in advance for your help :slight_smile: :smiley:

3 Likes

Hello @timngwanglong, Welcome to dataquest community

Please add full code of solution that would be help to better understand issue.

1 Like

Hello Dishin, thanks for your help. Please find full code below.

from csv import reader
file = open(“laptops.csv”)
file_reader = reader(file)
rows = list(file_reader)[1:]
price_to_name = {}
for row in rows:
price = int(row[2])
name = row[1]
if price in price_to_name:
price_to_name[price].append(name)
else:
price_to_name[price] = [name]
laptop1 = None
laptop2 = None
for row in rows:
price = int(row[2])
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(price)
print(laptop1)
print(laptop2)

Basically I have understood all till “elif” which really hard to understand.

1 Like

Hi @timngwanglong, please make sure to format your code appropriately instead of putting it in text form. In it’s current form, as you can also see, it’s difficult to figure out the indentation (which is extremely important in Python)

You can format your code by putting it between 2 sets of 3 backticks (```). So, something like

```
for some in something:
```

The above will automatically render into -

for some in something:

Also, to point out

You are assigning a value to price in your code -

price = int(row[2])
2 Likes

Hi the_doctor,

Thanks for your reply. Sorry i didn’t know how to use backticks earlier.

Please find full code below. I just do not understand when comes to “elif”.

Thanks for guiding me.

from csv import reader
file = open("laptops.csv")
file_reader = reader(file)
rows = list(file_reader)[1:] 


price_to_name = {}
for row in rows:
    price = int(row[2])
    name = row[1]
    if price in price_to_name:
        price_to_name[price].append(name)
    else:
        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(price)
# print the solution
print(laptop1)
print(laptop2)
1 Like

The statement checks if (5000 - price) is a key in price_to_name dictionary or not.

The above is not that different from -

if price in price_to_name:

It’s just checking if a particular value is a dictionary key or not.

1 Like

Please create a separate new post for this since it’s not relevant to the discussion here.

I am not sure how to do that can you please help me. Thank you

never mind, I figured it out. I removed the post from this page and created a separate new post