Challenge 4 Megathread

For any and all questions relating to challenge 4. :point_down:

For a tutorial on how to use Jupyter Notebook, we put together this video:

Still have questions? Read all the FAQs here.

That’s more like it! If you’re looking for a hints or alternatives, the enumerate() function is usually seen as a more “pythonic” way to access an index while iterating over a list.

11 Likes

Despite some typos, this question is a vast improvement upon the prior two/three.

29 Likes

Thanks for that share! Wish that was in the tutorial. Looks like a much cleaner way to handle it.

2 Likes

Same sentiment as the others - this time the question, code, and answers all correspond properly. Hooray.

7 Likes

Video Solution: https://youtu.be/TWq1_IPYN1E

7 Likes

I enjoyed today’s challenge. I think I’m starting to see a distinction between the data challenge vs the coding challenge. In the coding challenge, our code is the solution, whereas in the data challenge, we use code to aid us in our solution. Python is the tool, we use it to perform the laborious tasks and humans use the results of this to arrive at a conclusion. :thinking:

6 Likes

It is also important to try to incorporate in each challenge, what one has learned in previous ones

1 Like

It looks like they processed the list and get the product for the retail and the amount list and store that value in another list and then compare, I kinda went a bit further by adding if statements in my loop to get the item and if the wholesale buy was Yes or No, but Im guessing we’ll look at conditions later on…Good challenge!

5 Likes

I did the same thing and made the print statements match the answers. :stuck_out_tongue:

2 Likes

I did exactly the same thing. :slight_smile:

I’m attempting to solve the problem before looking at the solutions to at least have a bit of practice in the coding. If the answers are not what I produced, i’ll go back and play with my code to get what I need.

2 Likes

Spoiler Alert

I used a simple code to also format the output:

for i in range(len(item_list)):

item = item_list[i]
amount = amount_list[i]
wholesale = wholesale_price_list[i]
retail = retail_price[i]*amount_list[i]
buy_wholesale = "yes" if wholesale < retail else "no"

print(f"{item:<15}, amount: {amount:<5}, wholesale price: ${wholesale:<5}, retail price: ${retail:<5}, buy wholesale: {buy_wholesale: <5}")

The output looks like this:

Oak Wood , amount: 600 , wholesale price: $7000 , retail price: $7794.0, buy wholesale: yes
Blue Paint , amount: 150 , wholesale price: $1000 , retail price: $1348.5, buy wholesale: yes
White Paint , amount: 15 , wholesale price: $1000 , retail price: $149.85, buy wholesale: no
Paint Finish , amount: 165 , wholesale price: $800 , retail price: $658.35, buy wholesale: no

2 Likes

FYI, you can reference array elements from within the f string:

print( f"{item_list[i]}: {str(buy_wholesale)} -- costs {str(wholesale_price_list[i])} wholesale and {str(amount_list[i] * retail_price[i])} retail" )

I set buy_wholesale through logic similar to yours.

1 Like

A for loop is completely unnecessary for this challenge. It’s a simple matter of multiplying each element in amount_list by the corresponding element in retail_price, and then comparing the output to wholesale_price_list. I genuinely can’t see any way a for loop could be useful in this task.

2 Likes

multiplying each element in amount_list

Using the word each in the solution description typically means a for loop is exactly the solution. You genuinely can’t see how to it with loops, but I genuinely can’t see how to do it without. What if the lists got longer or shorter with new data? Would you have to change the code?

1 Like

I am totaly new to coding and thought this would be a good way to start. I am trying to learn as fast as i can but these challenges are going over my head in regards to coding. I have managed to fumble through the challenges by doing tutorials and using the extremely helpful info in the comments. Hopefully i can get a better sense of what im doing as the challenges move forward or im in trouble.

4 Likes

Heres my attempt with DataFrames:

import pandas as pd
df = pd.DataFrame({'Items': item_list, 'Amount': amount_list, 'WholeSale': wholesale_price_list, 'Retail': retail_price})
df['RetailPrice'] = df.Retail * df.Amount
df.loc[df['RetailPrice']>df['WholeSale'], 'BUY'] = 'YES'
df
1 Like

You would still multiply the elements in each list with each other, there really is no “need” for a for loop.