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
.