# Write a program that calculates the maximum of the values list without using a built-in function. Assign the maximum value to a variable named maximum

My Code:

``````values = [72, 48, 7, 66, 62, 32, 33, 75, 30, 85, 6, 85, 82, 88, 30, 32, 78, 39, 57, 96, 45, 57, 61, 10, 62, 48, 32, 96, 75, 15]

for i in range(len(values)):
if values[i] < values[i+1]:
maximum = values[i+1]
print(maximum)
``````

What I expected to happen:
It returns the desired answer for variable maximum, but how do I remedy the error using a while statement?

Replace this line with the output/error

``````---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-1-45c7bb6309e2> in <module>
2
3 for i in range(len(values)):
----> 4     if values[i] < values[i+1]:
5         maximum = values[i+1]
6 print(maximum)

IndexError: list index out of range
``````

The error occurs, but if i return maximum, i get the correct value. How do i remove that error using a while condition? I understand that if python iterates through the last index, the i+1 > the last index of the values list.

Hi

Define the `maximum` variable outside the `for` loop, and use it to compare in the `if` condition.

``````values = [72, 48, 7, 66, 62, 32, 33, 75, 30, 85, 6, 85, 82, 88, 30, 32, 78, 39, 57, 96, 45, 57, 61, 10, 62, 48, 32, 96, 75, 15]
maximum = 0
for i in range(len(values)):
if maximum < values[i]:
maximum = values[i]
print(maximum)
``````

Thanks.

@TravisBob: do you mean a while loop? Using a for loop also works as demonstrated by @dash.debasmita. Please remember to also include a mission link as per these guidelines when asking questions in the future.

``````i = 0
max = 0 # initializing the variable to store the max value
while (i < len(values)):
if values[i] > max:
max = values[i]
i += 1
print(max)
``````

remember that `i` will default to `0` in a `for` loop but you will need to initialize it in a `while` loop and also increment `i` by 1 for every iteration.

Outputs `96` as shown below:

You were trying to compare `i` to `i+1`. In the last iteration of the loop, the “element” at index `i+1` will be outside of the list (i.e. you are trying to access memory which you cannot access) thus python throws this error.

Hope this helps!

``````i = 0
max = 0 # initializing the variable to store the max value
while (i <= len(values)):
if values[i] > max:
max = values[i]
i += 1
print(max)
``````
``````---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-1-237020a8e67b> in <module>
17 max = 0 # initializing the variable to store the max value
18 while (i <= len(values)):
---> 19     if values[i] > max:
20         max = values[i]
21     i += 1

IndexError: list index out of range
``````

I tried setting the while condition to `<=` instead of `<`. I’m assuming the error occurs because the length of values is 30, and python index starts at 0, the 30th element is actually index 29, and `values[30]` would outside of index. So, when `i = 30`, `values[30]` will not execute?

Yes. That is correct as I mentioned earlier @TravisBob.