Get maximum value in the list Coding does not work as expected

Asked 1 years ago, Updated 1 years ago, 289 views

def maxFunc(t): 
    max = 0
    for i in range(10): 
        if t >= max:
            max = t
    return max

A = [1, 2, 3, 4, 5, 6, 73, 8, 10, 54] 

maxNum = list(map(maxFunc, A))

print(maxNum)

It's a code that prints the largest value among the internal values of the list, but I'm asking because the error code keeps coming up no matter how I fix it.
If you can, I'd appreciate it if you could include an explanation

python

2022-11-08 12:19

1 Answers

map() is not required, and range() is also useless.

A = [1, 2, 3, 4, 5, 6, 73, 8, 10, 54, 9, 9, 9, 9, 9]

def maxFunc(t): 
    max = 0
    For it: # <--- (2) Every time this is executed, i changes in order of 1, 2, 3, ..., 9
        Ifi >=max: # <--- (3) It compares well with max
            max = i
    return max

B = maxFunc(A) # <--- (1) Place A in the t position of the function maxFunc and solve the problem

print(B) # ---> (4) Works well


2022-11-08 20:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.