219k views
2 votes
8.11 LAB: Filter and sort a list

Write a program that gets a list of integers from input, and outputs non-negative integers in ascending order (lowest to highest).

Ex: If the input is:

10 -7 4 39 -6 12 2
the output is:

2 4 10 12 39
For coding simplicity, follow every output value by a space. Do not end with newline.

The answer needs to be in python. Can't figure out how to do this

User Mouradif
by
6.5k points

1 Answer

1 vote

nums = input("Enter your numbers: ")

lst = nums.split()

new_lst = ([])

for i in lst:

if int(i) >= 0:

new_lst.append(int(i))

new_lst.sort()

for x in new_lst:

print(x, end=" ")

The above code is in case the user enters the numbers.

def func(lst):

lst.sort()

for i in lst:

if i >=0:

print(i, end=" ")

lst = ([10,-7, 4, 39, -6, 12, 2])

func(lst)

The above code is in case you must input the numbers manually via a function.

I hope this helps!

User Faun
by
6.4k points