-
-
Save InquisitiveDev2016/87305372cf20a768cfc8ec8450bbb6d8 to your computer and use it in GitHub Desktop.
| """ 1.) Understand the problem | |
| 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. | |
| 2.) Plan a solution | |
| Algorithm: | |
| - Create new empty list | |
| - For loop to iterate over each element | |
| - If element < 5: | |
| - Append the element into new list | |
| - Print the new list | |
| 3.) Carry out the plan | |
| """ | |
| a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] | |
| new_list = [] | |
| for item in a: | |
| if item < 5: | |
| new_list.append(item) | |
| print(new_list) | |
| """ 4.) Examine your results for accuracy: | |
| b = [1,1,2,3] | |
| """ |
I've done the same but I've got multiple lists. Why is that?
[1]
[1, 1]
[1, 1, 2]
[1, 1, 2, 3]
[1, 1, 2, 3, 5]
I've done the same but I've got multiple lists. Why is that?
[1]
[1, 1]
[1, 1, 2]
[1, 1, 2, 3]
[1, 1, 2, 3, 5]
This is very old code (over 3 years ago) and I don't even remember writing this stuff lol.
Try this solution instead:
list = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
for item in list:
if item < 5:
print(item)
Make sure you are tabbing and using whitespace properly in Python, otherwise it will not work properly.
It will give you the following output:
1
1
2
3
#Program for List Less Than entered number
new_list = []
number_list = [4, 6, 7, 13, 14, 15]
input_number = int(input("Enter a number for list\n"))
if input_number not in number_list:
print("Entered number is not present in listing")
else:
for number in number_list:
if number < input_number:
new_list.append(number)
print(new_list)
a = [1,1,2,3,5,8,13,21,34,55,89]
for number in a:
if number < 5:
print(number)
Output is
1
1
2
3
Process finished with exit code 0
I've done the same but I've got multiple lists. Why is that?
[1]
[1, 1]
[1, 1, 2]
[1, 1, 2, 3]
[1, 1, 2, 3, 5]This is very old code (over 3 years ago) and I don't even remember writing this stuff lol.
Try this solution instead:list = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] for item in list: if item < 5: print(item)Make sure you are tabbing and using whitespace properly in Python, otherwise it will not work properly.
It will give you the following output:
1
1
2
3
you need to remove the print(item) from the for loop.
I've done the same but I've got multiple lists. Why is that?
[1]
[1, 1]
[1, 1, 2]
[1, 1, 2, 3]
[1, 1, 2, 3, 5]
make sure that print should be outside the loop..
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b=[]
for i in a :
if i <= 5 :
b.append(i)
print(b)
this will give you perfect results.........print is out of loop.
[1, 2, 3, 4, 5] is the output.
bekaar