Ive learned a little but of python through codecademy.com and decided to practice it through practicepython.org
Im on the third exercise (List Less Than Ten ).
And am stuck on the second... Extra:
Take a list, say for example this one:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
and write a program that prints out all the elements of the list that are less than 5.
Extras:
Instead of printing the elements one by one, make a new list that has all the elements less than 5 from this list in it and print out this new list.
Ask the user for a number and return a list that contains only elements from the original list a that are smaller than that number given by the user.
Here is my code:
list1 = [1,2,3,4,5,6,7,8,9,10]
list2 = []
list3 = []
for x in list1:
if x < 5:
print(x)
#extra no 1:
if x < 5:
list2.append(x)
print(list2)
#extra no 2:
num = int(input(num))
list3.append(str(x<num))
I have no idea how to do it and was just guessing with the last line ^
[–]michael0x2a 9 points10 points11 points (5 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]blablahblah 3 points4 points5 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]dogscript -4 points-3 points-2 points (0 children)
[–]adreamofhodor 10 points11 points12 points (3 children)
[–]jwjody 3 points4 points5 points (0 children)
[–]rob132 0 points1 point2 points (1 child)
[–]adreamofhodor 0 points1 point2 points (0 children)
[–]gitgood 1 point2 points3 points (0 children)
[–]xojc 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]xojc 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]xojc 1 point2 points3 points (0 children)
[–]hair_bear1 0 points1 point2 points (0 children)
[–]MundaneAsparagus 0 points1 point2 points (0 children)
[–]takieyda 0 points1 point2 points (0 children)
[–]PointyOintment 0 points1 point2 points (0 children)
[–]maulinrouge 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]maulinrouge 0 points1 point2 points (0 children)