Evojr
Evojr

Reputation: 21

How do I move to the next item in a list using loops?

I need to create a subroutine in python which identifies the highest number in a list, but I don't know how to move to the next item in the list using a loop

I've tried to add 1 to the counter, but I am not entirely sure of the of the syntax

number = []
for i in range(0, 5): # set up loop to run 5 times
    numbers = int(input('Please enter a number: '))
    number.append(numbers) # append to our_list
tempmax = number[0]
maximum = 0
for i in number:
    if tempmax > maximum:
        maximum = tempmax
        tempmax = number[+1]
    else:
        tempmax = number[+1]

print(maximum)

This is what I've tried, but I don't really know how to fix it. Any tips would be appreciated.

Upvotes: 2

Views: 5188

Answers (4)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24153

A for loop will give you each item in the sequence you are iterating over.

You named the input number numbers and your list of numbers number. Swap those names around to be clearer.

You are iterating over a list of numbers so each item is a number. You named it i which probably made you think it was an index, but it's not, it's the actual number. Give it a good name:

for number in numbers:

Then you want to test whether the number is the maximum:

    if number > maximum:
        maximum = number

Upvotes: 1

alberand
alberand

Reputation: 662

Possibly you are looking for max(numbers). It will return the biggest number in the list.

If you want to get index of the number after maximum, try numbers.index(max(numbers)) + 1

a = [1, 2, 3, 4, 1, 2, 3]
>>> max(a)
4
>>> a.index(max(a))
3
>>> a[a.index(max(a)) + 1]
1

Note: list.index() return index of the first maximum.

Upvotes: 0

IMCoins
IMCoins

Reputation: 3306

As the for loop seeks the next item automatically and loads it in the variable you asked it to... for YOUR_VAR in YOUR_ITERABLE, you either need to work on the items you want, or on the indexes.

So, if your items were arr = [10, 20, 30, 40, 50], you could access them both ways.

for item in arr:
     print(item) # Will print successively 10, then 20, then 30, then 40, then 50.

array_indices = len(arr)
for index in array_indices:
    print(index) # will print successively 0, 1, 2, 3, 4
    print(arr[index]) # will print successively 10, then 20... 30, 40, 50

Upvotes: 0

taurus05
taurus05

Reputation: 2546

Is this want you to want to do?

number = []
for i in range(0, 5): # set up loop to run 5 times
    numbers = int(input('Please enter a number: '))
    number.append(numbers) # append to our_list
tempmax = number[0]
maximum = 0
for i in range(len(number)-1):
    if tempmax > maximum:
        maximum = tempmax
        tempmax = number[i+1]
    else:
        tempmax = number[i+1]

print(maximum)

If you want to determine maximum value in a list, you can also do something much easier like this (without having to recreate the wheel).

max_val = max(number)
print(max_val)

Upvotes: 0

Related Questions