Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if min is not
#1
Hello,
I start in python and I don't understand the line (if min is not en_cours :) in this sorting by insertion program:

A = [8,6,15,9,2,1,99,5,7]
print (A)
nb = len(A)
for en_cours in range(0,nb):
    plus_petit = en_cours
    for j in range(en_cours+1,nb) :
        if A[j] < A[plus_petit] : 
           plus_petit = j
    if min is not en_cours :
        print (min)
        temp = A[en_cours]
        A[en_cours] = A[plus_petit]
        A[plus_petit] = temp
print(A)  
thank for your help Thierry
Reply
#2
there are multiple issues in this code.
min is built-in function, that is why this code doesn't produce an error. You should not use built-in functions as names.
then you need to define whatever min is supposed to be (i.e. instead of min use some other name and define that name in advance). Let say you replace min with num.
it looks like you wanted to test for inequality, not for identity. is is checking for identity (i.e. both are same object with same id), == checks for eqaulity (same value, not necessary same object), != checks for inequality`
you should use if num != en_course:

If the result will be what you expect - make the changes and see
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020