For loop in 4.3 page 7 of 13

Looking for a hint on this one, I’m using a for loop to iterate through the list but struggling to figure out how to use 2 variables to compare the number from the previous iteration with the current one.

Do I need to use a “while” loop to do this?

I’ll take a crack again tomorrow, just looking for a nudge in the right direction

Hint: try finding a way to use an “if” and a “>=” for each value in the list.

1 Like

Oh man! This one has me stumped as well. Did you get it to work?

This one is definitely tricky, but think about this part of the instructions:

Second, we need to search for the largest entry in the list and print it to the screen. We’ll need a variable to hold the largest value found so far, and update that variable whenever another number in the list is greater than it.

Emphasis is mine. If we break that down into some numbered psuedocode steps, it’d look something like this:

  1. Create a variable to hold the largest number you’ve seen so far.
  2. Compare each number in the list to see if that is the new highest number.
  3. If a number is the new highest number, update your highest number variable.

See if that helps out any. One of the tricky parts of this is where should you create the variable to hold your largest number and what value you should start that variable at.

I am going to try and apply this.

Honestly though, I try to apply the fill in the blank information, as sort of like a template and, I do not see them assigning a variable to the range. So maybe, I should scrap the use of the fill in the blank scheme …

"One of the tricky parts of this is where should you create the variable to hold your largest number and what value you should start that variable at."

This is definitely one of my problems, are we to write the program knowing that the list only has integers between -999 and 999? if so that’d be a big help.

at work now but i’ll play with this more tonight

@Zburmaster: Yes, you’re right about that being part of the design specification for this program. If you read the instructions again, the first bullet is:

First, we need to create a list of integers (positive or negative) with values between -999 and 999.

So, indeed, you can write your program with that constraint in mind. :slight_smile:

I figured it out, mine was very close to the answer given - I had written a second conditional for when the first is false, I found out this is redundant, python would have done this step automatically when the first if is false. It did help me think through the problem though. I’d put up the code but I’m not sure if we’re doing that here.

Lesson learned: if you are stuck, re-read the prompt.

Thanks for the help!


Hi, I just started this course and have a question about same coding assignment:

  1. why do we assign bellow the min range -1000 to the largest instead of actual minimum in a range : -999 or the first number in the given range like -1? The result is the same
  2. Order matters! Why? number0=number, not number=number0. It means same thing to me, but Python reads it differently and gives different result.
    I am trying to understand the logic so I can follow it in future, thank you in advance!

To address the second question-- that has to do with the way Python assigns values. When we write things this way, it assigns the value of the second variable to the first variable. So, you can imagine what might happen if you reversed that.

We are assigning values this way, not proving symmetry, as in math.

Yes, it makes sense to me now when I read it.
Thank you

1 Like

Very cool! I am glad to hear that all makes more sense now.

We assign the largest variable a value of -1000 to make sure that it will be replaced by a value actually found in the list. We’ve been told that the numbers in the list will all be bigger than that, so by setting the variable to initially hold -1000, we know that our code can’t accidentally print out the default value.

1 Like

Makes perfect sense! Thank you!
In our case we see all numbers that are in the list, but if we have longer list, we would not be able to identify largest from just looking at them. To ensure we include all range we would start with -1000.