Home > Mobile >  how to enter elements using "input"
how to enter elements using "input"

Time:11-27

EDIT: command "lista = [int(i) for i in input("Podaj Liczby: ").split(",")]" still doesnt sort When I try to enter numbers, it does not sort them for me Even if i use "," still doesn't sort here's code:

lista = [int(i) for i in input("Podaj Liczby: ").split(",")]
def sortowaniebabelkowe():
     n = len(lista)
     zmiana = False
     while n > 1:
         for l in range(0, n-1):
             if lista[l]>lista[l 1]:
                 lista[l],lista[l 1]==lista[l 1],lista[l]
                 zamien = True
         n -= 1
         print(lista)
         if zmiana == False: break
sortowaniebabelkowe()

CodePudding user response:

i think you have to use

lista[l],lista[l 1]=lista[l 1],lista[l]

insead of

lista[l],lista[l 1]==lista[l 1],lista[l]

to swap the values.

CodePudding user response:

I modified above code in to this form

lista = [int(i) for i in input("Podaj Liczby: ").split(",")]
def sortowaniebabelkowe():
     n = len(lista)
     while n >0:
         for l in range(0, n-1): 
             if lista[l]>lista[l 1]:
                 lista[l],lista[l 1]=lista[l 1],lista[l]
         n -= 1
     print(lista)     
sortowaniebabelkowe()

Now it works.

CodePudding user response:

You have several problems in your sorting function.

  1. == is a conditional operator and not an assignment operator.
  2. The name of your variable zmiana is not same. This implies, that zmiana is never True so the while loop only runs once.

The updated code:

lista = [int(i) for i in input("Podaj Liczby: ").split(",")]
def sortowaniebabelkowe():
     n = len(lista)
     zmiana = False
     while n > 1:
         for l in range(0, n-1):
             if lista[l]>lista[l 1]:
                 lista[l],lista[l 1]=lista[l 1],lista[l] # == is replaced with =
                 zmiana = True #The name of the variable is changed to zmiana
         n -= 1
         print(lista)
         if zmiana == False: break
sortowaniebabelkowe()
  • Related