# Unable to understand the "if" and "elif" section of this program

Hi,
I understood most of the program, but I’m stuck at the “if” and “elif” section. Please explain it.

``````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)
``````

https://app.dataquest.io/m/1011/python-fundamentals-practice-problems/7/laptop-purchase

Thank you very much

Hi @vtwinhead, welcome to the community!

The way that the `price_to_name` dictionary was created in the solution ends up giving you dictionary items where the key is the price and the value of the key is a list that contains one or more laptop names. I’ll print a few of them just for visual aids, because I think it’s important to understand how the dictionary looks to understand the if/elif statements:

``````{174: ['C740-C9QX (3205U/2GB/32GB/Chrome'],
192: ['Vivobook E200HA'],
196: ['V131 (X5-Z8350/4GB/32GB/FHD/W10)'],
199: ['E402WA-GA010T (E2-6110/2GB/32GB/W10)', 'Chromebook C910-C2ST'],
203: ['K146 (N3350/4GB/32GB/W10)'],
209: ['Chromebook 15', 'Stream 11-Y000na'],
...
``````

Let’s look at the 209 key. `price_to_name[209]` has the value of `['Chromebook 15', 'Stream 11-Y000na']`. If I wanted to access just one of these laptop names, I need to use the index value. That means:
`price_to_name[209][0] = 'Chromebook 15'`
and
`price_to_name[209][1] = 'Stream 11-Y000na'`

Additionally, this would mean that `len(price_to_name[209]) = 2`, because there were 2 items in the list.

For key `192`, `price_to_name[192]` has a value of `['Vivobook E200HA']`. This means that `price_to_name[192][0] = 'Vivobook E200HA'`.

So now let’s look at the if/elif statements.

The first scenario is the possibility that we have 2 laptops that both have a price of 2500. The if statement is checking for 2 things: first, that the price is 2500 for that row in the dataset, and secondly if there are 2 or more laptops at that price from the dictionary. If both conditions pass, then we can assign the `laptop` variables to those values.

The second scenario would be to find 2 laptops with different prices but add to 5000. Let’s say `price = 2300` – then we’re looking in the dictionary to see if the key `2700` (5000-2300) exists in the dictionary. If it does, then that means `laptop1 = price_to_name[2300][0]` and `laptop2 = price_to_name[2700][0]`. (We use `[0]` for both, because even if there are 2 laptops at either price, we only need one of them and every key has at least 1 item in the list.)

I hope that helps!

2 Likes

It definitely helps! Thank you so much.

Hi @april.g , just following up with your explanation. I have difficulty to understand the solution to create the dictionary. Why we can’t just use the code to create the dictionary like we did in the previous questions?

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

1 Like