Act fast, special offers end soon! Up to $294 is savings when you get Premium today.
Get offer codes

Conditional Assignment

Screen Link: Learn data science with Python and R projects

fire_first = fire_only[0]
water_first = water_only[0]

for rows in fire_only:
    if rows[23] < fire_first[23]:
        rows[23] = fire_first
        fire_starter_pick = fire_first[0]
        print (fire_starter_pick)
          
for row in water_only:
    if row[23] < water_starter_pick[23]:
        row[23] = water_first
        water_starter_pick = water_first[0]
        print (water_starter_pick)

I am extremely new to python here. Can someone explain how to find the min and max value without using min and max. Furthermore, how do you find the name of the value?

Hi @kwong.victoriaa and welcome to the community!

Unfortunately I cannot access the link you provided in order to get some more context for your question. It appears to be one of the new assessments and so, perhaps, it is in beta testing and only a handful of people currently have access to it and you are one of them! :sunglasses:

That said, I will try to answer your question as best I can without having access to this screen.

One strategy here would be to initialize a variable with the first element in your list and then loop through your entire list to compare each element to this variable. If you come across an element that is greater/less than this variable, update the variable with this new element.

I’m not quite sure what you’re asking here…can you explain this a little more?

EDIT_1:
I helped to create some of these assessments and as such was able to find the problem you’re working on so I now have a better understanding of what you are trying to do here.

It looks like you’ve got the right idea with your code but there’s a couple things we should take a look at. In particular, this chunk of code:

for rows in fire_only:
    if rows[23] < fire_first[23]:
        rows[23] = fire_first

Here you are doing what I suggested above (loop through the list and compare to see if it’s less than) however the last line (rows[23] = fire_first) is trying to assign the value in fire_first to the 23rd item in rows. We actually want to do the opposite: assign the value in the 23rd item of rows to the variable fire_first. In other words, we should swap this around to: fire_first = rows[23].

It appears that the values stored in column index 23 are not numeric so you will need to convert these values to an int or a float before using a comparison operator like <.

Also, you may want to put the print statements outside the for loop so that you aren’t getting so much printed to the console.

Wow! Cool! I didn’t know that. I find this set of assignment much harder than the previous assignment for other topics. I have modified my code a little bit. But somehow my answer is still wrong :disappointed_relieved: I am getting 2 answers for the water type. Somehow my indentation are not showing…

fire_first = fire_only[0]
water_first = water_only[0]

for rows in fire_only:
    if rows[23] < fire_first[23]:
        fire_starter_pick = rows[0]
        print (fire_starter_pick)

for row in water_only:
    if row[23] < water_first[23]:
        water_starter_pick = row[0]
        print (water_starter_pick)

The reason indentations are not showing correctly is because we need to enclose chunks of code between a set of triple backticks (```). If not, markdown treats it as regular text and will automatically remove white spaces. I have edited your message to implement this for you. For more great tips and tricks on posting questions in the community, you may want to read this post.

This is to be expected since assessments are not as “guided” as regular lessons and require a bit of trial and error before we land on a solution. Out of curiosity, do you feel that this assessment is too hard for where you are on your learning path or is it just the right amount of “struggle?”

WELL…What an adventure this has been! I realized that in order to help you any further, I would need access to the dataset but since these assessments are still in beta testing, only a select few special people have access to them (like you, but not me!) I could not download the file (“pokemon starter.csv”) directly from the DQ platform like I could for any other regular lesson. So I tried to “get around” this by using the tool that us authors-of-assessments use to create the assessments in order to access the pokemon dataset from the “inside.” I really shouldn’t have attempted that until I finished my first morning coffee! I inadvertently discovered a bug, managed to foul up the file system, and still didn’t get the file! In the end, I managed to get the file with the help of @Bruno who also graciously cleaned up my mess. You’re my hero!

Once I did get my hands on “pokemon starter.csv” and played around with it (in conjunction with your code) I have finally figured out where things are going astray for you. Let’s dive in!

First thing is that we are comparing rows[23] to fire_first[23] and when it is less than (ie <) we are not updating the fire_first variable for future comparisons. This is not the strategy we want because fire_first started (randomly) as the first element of fire_only just so we had something to compare to. If we don’t update this variable, we will continue comparing new Pokemons to this first entry. When we find an evolution time that is less than (column 23) we want to update our fire_first and water_first variables for future comparisons. For this reason, I changed the names of these variable to reflect that they are storing evolution min times. What I would suggest is to use something like this:

fire_min = fire_only[0]

for rows in fire_only:
    if rows[23] < fire_min[23]:
        fire_min = rows # this will update the variable to a new `min` for future comparisons
        fire_starter_pick = rows[0] 

print(fire_starter_pick)

Also, notice how I removed the print statement from inside the for loop. This is why you were seeing two names for the water_only list: the if condition was being met twice and therefore printed two different names. Technically, we don’t need to print the names here. I think it’s better to update the fire_starter_pick and water_starte_pick variables inside the for loop and once it is complete, print these variables outside the loop.

I hope this isn’t too much to follow but if it is, just let me know and we can work on it together some more.

1 Like