Python for

Asked 2 years ago, Updated 2 years ago, 106 views

y = 0
for i in range(4):
    Text(Point(-0.3, y), y).draw(win)
    y += 10
    max = ????

What I don't know is that I don't know what to do with the above max

Like that, y would be 0, 10, 20, 30

I want to print 30 when I do print(max).

However, if the number in range brackets changes to 6, for example,

So that the max value can also change to 50.

What should I do?

python for

2022-09-22 14:08

1 Answers

Python has a max() function. This function returns the largest value of the entered factor.

If you compare the previous large value with the current large value for each loop, you can eventually get a large value for all loops.

max overlaps with the function name, so if you change it to maximum, it is as follows.

y = 0
maximum = 0
for i in range(4):
    Text(Point(-0.3, y), y).draw(win)
    maximum = max(y, maximum)
    y += 10

print(maximum)


2022-09-22 14:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.