F
F
Feor_slen2021-12-15 19:38:15
Python
Feor_slen, 2021-12-15 19:38:15

The condition statement does not work in a function with return, how to fix it?

The function returns not None only if the numbers already arranged in the correct order are run through it. The task of the function to sort from largest to smallest is completely confused (yes, I understand that this code is one big complication, but it needs to work in this form).

def terra(num):
    exit = len(num) - 1
    for i in range(len(num)-1):
        if num[i] < num[i + 1]:
            b = num[i + 1]
            num.pop(i + 1)
            num.insert(i,b)
        elif num[i] >= num[i + 1]:
            exit = exit - 1
    if exit == 0:
        return num
    else:
        terra(num)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Feor_slen, 2021-12-15
@Feor_slen

I figured out where I went wrong:

if exit == 0:
    num.pop()
    return num
else:
    return terra(num)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question