Python Forum
this small code dosento work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
this small code dosento work
#1
Hi guys

im just learinig python so the task is to pass throu list a and print only numbers that are bigger than 5. my code isnt working and i dont know whats wrong;

here is the code

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
for i in a:
if i < 5:
print(i)

the error msg i get:

File "C:/Users/juanb/PycharmProjects/test/tes1.py", line 3
if i < 5:
^

IndentationError: expected an indented block

Process finished with exit code 1


thanks a lot !
juan
Reply
#2
Indentation is part of python syntax, so:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
for i in a:
    if i < 5:
        print(i)
Also this does the opposite of what you asked:
Output:
1 1 2 3
Reply
#3
is this what you wanted?
>>> a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> for i in a:
...     if i < 5:
...         print(a[i])
...
1
1
2
3
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 767 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 674 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Beginner: Code not work when longer list raiviscoding 2 812 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,770 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,427 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 881 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,228 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  small code for sampel asn1ate borys 0 857 Jul-26-2022, 10:48 AM
Last Post: borys
  cannot get code to work Led_Zeppelin 10 2,419 Jun-30-2022, 06:28 PM
Last Post: deanhystad
  How does this code work? pd_minh12 3 1,334 Apr-15-2022, 02:50 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020