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))
PeterPefi
  • 87
  • 1
  • 1
  • 7
  • Please include the error message. Python showed you the line with the error, seems kinda rude not to share! – tdelaney Apr 10 '20 at 22:28

2 Answers2

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.

iz_
  • 15,923
  • 3
  • 25
  • 40
  • 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. – PeterPefi Apr 10 '20 at 22:23
  • Though if I DO edit the code like that, it just return None – PeterPefi Apr 10 '20 at 22:23
  • @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`). – iz_ Apr 10 '20 at 22:24
  • so i would contain the value of the element while in the for-loop and not the counting index? – PeterPefi Apr 10 '20 at 22:30
  • Oof yeah it was just the return I forgot, thank you kindly. – PeterPefi Apr 10 '20 at 22:30
  • Yes. This is easy to test with `for i in b: print(i)`. – iz_ Apr 10 '20 at 22:30
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
Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29
  • 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? – PeterPefi Apr 10 '20 at 22:28
  • 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`. – iz_ Apr 10 '20 at 22:29
  • `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](https://stackoverflow.com/questions/2568783/python-why-does-random-randinta-b-return-a-range-inclusive-of-b) – Susmit Agrawal Apr 10 '20 at 22:31