I'm trying to get a function where if you do sort(listname) it would sort all the numbers inside that list from least to greatest.
I'm not sure whats wrong with mine, but I need some help as the output isn't actually least to greatest it does least to greatest for the first two numbers of the number.
Example :
If list has 23, 212, 44 in it than I sort it the output will be like this.
Output :
212,23,44
It should be 23, 44, 212.
Code:
def sort(my_list):
size = len(my_list)
for i in range(size):
for j in range(size-i-1):
if(my_list[j] > my_list[j+1]):
tmp = my_list[j]
my_list[j] = my_list[j+1]
my_list[j+1] = tmp
More code :
numbers=([])
amount=input("How many numbers are in your list? ")
print("")
counter = 0
ran = 0
while counter < int(amount):
counter = counter + 1
ran = ran + 1
num3 = input(str(ran) + ". Input: ")
try:
val = int(num3)
except ValueError:
num3 = input(str(ran) + ". Input: ")
sort(numbers)
numbers.append(num3)