1

I don't get how a "for" loop that iterates through the elements of a list can be out of range. First part seems to be okay as I can print it.

import random

def random_list(n):
    l = []
    for i in range(0,n):
        l.append(random.randint(0,n))

    return l


def maximum(n):
    x = 0
    b = random_list(n)
    for i in b:
        if b[i] > x:
            x = b[i]


print (maximum(10))
1
  • Please include the error message. Python showed you the line with the error, seems kinda rude not to share! Commented Apr 10, 2020 at 22:28

2 Answers 2

3

This:

for i in b:
    if b[i] > x:
        x = b[i]

Iterates over the elements of b, not the indices. Change it to

for i in b:
    if i > x:
        x = i

You also need to return something from your function, probably x.

Sign up to request clarification or add additional context in comments.

6 Comments

I may have worded poorly, the idea is to fetch the maximum number in a list, therefore i thought I needed the elements in b not the index.
Though if I DO edit the code like that, it just return None
@PeterPefi Yes, but for i in b iterates over the elements. Compare that to for index in range(len(b)):. Also, you need to return something from your function (probably x).
so i would contain the value of the element while in the for-loop and not the counting index?
Oof yeah it was just the return I forgot, thank you kindly.
|
0

Considering that you know how to iterate over a list in python, the error could be due to randint, which generates an integer in the interval [low, high]. This means high is a possible output, while the highest index in your program is high - 1.

For example,

random.randint(0, 0)

gives 0.

Similarly, random.randint(10) can return 10.

If you don't understand how to iterate over a list in Python, consider a simple example:

Take the list below:

myList = [1, 3, 5, 7, 9]

Now, there are two ways to iterate over the list:

  1. Directly accessing elements:

    for element in myList:
        print(element, end=" ")
    

This gives the output:

1 3 5 7 9
  1. Accessing elements using indices

    for idx in range(len(myList)):
        print(idx, ":", myList[idx], end=" ")
    

This gives the output:

0:1 1:3 2:5 3:7 4:9

3 Comments

I fear this might have flown over my head. Wouldn't the range function limit the range to 0,n(exclusive) so basically n-1, which should be fine?
The randomly generated numbers aren't an issue in this case. Also, for the second scenario in this answer, it would definitely be better to use enumerate.
randint includes n as a possible generated number; its range is 0(inclusive) to n(inclusive). https://stackoverflow.com/questions/2568783/python-why-does-random-randinta-b-return-a-range-inclusive-of-b

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.