There's a problem with the repeat statement

Asked 1 years ago, Updated 1 years ago, 98 views

import random

lst = list()
for i in range (0,10,1) :
    lst.append(random.randint(0,100))

print(lst)

maxidx = 0
for i in range(0,10,1):
    if lst[maxidx]<lst[i] :
        maxidx=i

tmp =lst[maxidx]
lst[maxidx] = lst[9]
lst[9] = tmp

print(lst)

maxidx = 0
for i in range(0,9,1):
    if lst[maxidx]<lst[i] :
        maxidx=i

tmp =lst[maxidx]
lst[maxidx] = lst[8]
lst[8] = tmp

Find a large number and change the position with the element on the right by the way I'm going to repeat this to make it list from the left to the small number to the big number How do I make a repeat sentence?

for loops list

2022-09-22 18:34

1 Answers

If you want the result, You can do lst.sort()

If you're going to practice, write a double for statement

for i in range(0,9,1):
   maxidx = 0
   for j in range(0,9-i,1):

Why don't you write a repeat in the same way?


2022-09-22 18:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.