-1

I am new in python and I am trying to make a simple cicle for this my code:

for i in range(0,5) :
    if i==0 :
    b=b.append(1)

else: 
result=(b[i-1]+1) 
b.append(result)
return(result)

File "", line 5 b=b.append(1) ^ IndentationError: expected an indented block

how can I fill a vector or matrix with my results?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • You'd need to show some properly formatted code to get help with this one. Edit your question, highlight the code block, and click the `{}` button in the toolbar to format it. – Mad Physicist Apr 28 '18 at 10:08
  • move the lines below the `if` and the `else` a 4 spaces (or 1 tab) to the right – shahaf Apr 28 '18 at 10:08
  • 1
    You might want to start with [a tutorial](https://www.python-course.eu/python3_blocks.php) to understand the basic principles. – Mr. T Apr 28 '18 at 10:11
  • Format the exception as code as well. You can put `

    ` to create a visual break between code and exception. It helps to see everything that comes off the console in monospace.
    – Mad Physicist Apr 28 '18 at 10:13
  • 1
    @BenyaminJafari When editing 1) don't just apply your own favorite code style and 2) don't apply changes to code that will invalidate the question and answers. The specific problem of the question was due to the incorrect indentation that you 'fixed' with your edit. – Mark Rotteveel Apr 28 '18 at 15:49
  • 1
    @MarkRotteveel: and it got approved by two reviewers ... – Jongware Apr 28 '18 at 15:54

2 Answers2

1

Because you didn't indent your code correctly. It should look probably like this:

for i in range(0,5):
    if i==0 :
        b=b.append(1)
    else: 
        result=(b[i-1]+1) 
        b.append(result)
return(result)

where the body of the for loop is indented, and so do the bodies of the if and else statements, relatively to their respected statements.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Thanks , I tryied to write a code as you suggested but now i am getting another error: b=b.append(1) AttributeError: 'NoneType' object has no attribute 'append', – Andrea Bencini Apr 29 '18 at 10:50
0

You're getting the error because python uses white space to determine when blocks start and end. You have to use indenting to open \ close all of your logic blocks.

if thing > thing2
    inside_if_stuff = 1

the_if_has_now_ended_and_Im_doing_other_stuff 

There's no characters like {} to open and close logic in python

Mason Stedman
  • 613
  • 5
  • 12