N
N
Nazar Pektropupovich2020-05-25 20:14:29
Python
Nazar Pektropupovich, 2020-05-25 20:14:29

Using Python list length?

When writing the code, the task was set to indicate in the number of repetitions of the cycle - the number of elements of the list (The user fills in the list himself) I
enclose the code:

def min2(*a):
    k = a[0]
    kk = 1

    kilkist = len(a)
    while kilkist != 0:
        if k < a[kk]:
            k = a[kk]
            kk = kk + 1
            kilkist = kilkist - 1
        else:
            kk = kk + 1
            kilkist = kilkist - 1
    return k

print(min2(Тут идут рандомные числа))


How to write correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2020-05-26
@phaggi

Since in python indexes start from zero, and you iterate from the first, then at the end of the loop the counter will exceed the maximum index and there will be an error.
In addition, in the current form, this is not min, but max.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question