-3

I want to get list length and the corresponding elements as the input.

      int(input("Please enter how long the list is: "))
      my_list = [56,3,89,2,34,12]
   def my_sort(l) :
my_sorted = False
sorted_pos= len(l)-1


print(l)
while not sorted_list:
   sorted_list= True 
   for index in range(sorted_pos):
       if my_list[index] > my_list[index +1]:
           sorted_list= False
           my_list[index], my_list[index +1] = my_list[index +1], my_list[index]
           print(l)
amin
  • 1,413
  • 14
  • 24

1 Answers1

0

You probably wanted this:

elems   = int(input("Please enter how long the list is: "))
my_list = []

for i in range(elems):
    elem = int(input("Please enter next element: "))
    my_list.append(elem)

def my_sort(l):
    sorted_list = False
    sorted_pos= len(l)-1

    print(l)

    while not sorted_list:
       sorted_list = True 
       for index in range(sorted_pos):
           if l[index] > l[index +1]:
               sorted_list = False
               l[index], l[index +1] = l[index +1], l[index]
    print(l)

my_sort(my_list)

Note that the indentation in Python is important.

In the first statement you get the number of items from the user and save it in the variable elems.

Then in the for loop you ask the user put individual elements and append it to (originally empty) list my_list.

In the next step you create (define) the function my_sort which takes as argument a list of numbers. You named this argument as l. This function print the given list, sort it end finally print sorted list.

Your last step is to call this function with your list my_list instead of formal argument l.

MarianD
  • 13,096
  • 12
  • 42
  • 54