"Expected an indented block" What to do? - python

I'm new to programming and don't know what this means. Please tell me what to do to my code for my assessment. It doesn't highlight the problem when I run it and I don't know what to do. Please help me.

To be honest, reading a Python tutorial would probably do you better. In any case, however...
Python is an indentation-based language. This means anytime you enter a new "block" (code that belongs to a statement), you need to indent your code by one level. Here are two examples, one incorrect and one correct:
if 5 < 10:
print "5 is less than 10! Wow! Thanks, math!"
print "I'm so glad Python told me."
This is a syntax error, since the print statement belongs to the if statement, and is therefore a new block. It should be indented, but in this case it wasn't, so it's an error.
if 5 < 10:
print "5 is less than 10! Wow! Thanks, math!"
print "I'm so glad Python told me."
print "This is printed in any case, since it doesn't belong to the above block."
Here is the fixed verion. Notice the four spaces in the beginning of the second line? That's called "indentation". Any subsequent lines indented to the same level will be part of the block. Generally, you press TAB to indent in your text editor. The last line, however, is not indented and will therefore run regardless of whether the if statement evaluates to True or not.

Related

Avoiding indentation in Python else and elif

In writing a Python code in Wing or IDLE, inside the if condition, we must have an indentation for each line. But the else and the elif part must be aligned with if. However when I type else: or elif: in a newline, it has an indentation and I must remove it manually. I have seen it in several IDEs like Wing.
Is there any way to avoid indentation for else and elif? Indeed I need this:
if (condition):
do this
else:
do this
But when I type it, it's like:
if (condition)
do this
else:
do this
You seem to believe the editor will somehow know how many lines the indented suites of code should have.
It would be easy to make editors "outdent" if those suites were all one line, but a cursory inspection of any moderately complex Python program will show that is clearly not the case.
Which means it's up to us to indicate the end by outdenting manually, usually with SHIFT-TAB, at the end of the clause.

Problems with Syntax and indentation errors

I'm going through a Book which, at this point in the book, requires me to make a small videogame that calls functions, uses if's, while's -- essentially all the things covered in the book so far. But, I get this error in this part of my code:
Code edited, get a new error.
File "ex35_study.py", line 24
third_scenario_code()
IndentationError: unindent does not match any outer indentation level
Here is my code:
options_thirdscenario_actions = ['Examine the door', 'Try to force it']
def third_scenario_code():
print "Let me try to crack this thing up, says Lars as he starts to type in the panel. You hear the sounds of the fight out there, there's not much time left. "
print "After receiving several commands a window with a code pop ups. "
print codefile.read()
def third_scenario():
print "You two get out of the cell and approach to the exit, a long corridor is ahead of you, flashing red lights indicate an state of emergency, you must evacuate."
print "As soon as you two approach to the door, it closes"
print "Crap it must be the emergency system, we have been detected"
next = raw_input("What do you do> ")
if next == 'Examine the door':
print "A small panel comes out, requires to enter a code of words"
third_scenario_code()
elif next == 'Try to force it':
print "You try to force the door with no result"
print options_thirdscenario_actions
next2 = raw_input("What else do you do> " )
if next2 = 'Examine the door'
third_scenario_code()
else:
print "You already did that"
I am getting a similar error on the whole program and I suspect it has something to do with indentation, but I have tried every suggestion I see in google with no fruitful result. thanks in advance.
You are missing colons after the one of if conditions and need to line things that are the same scope up, i.e. the print after the function call but you may also be mixing spaces and tabs. It is recommended to always use 4 spaces rather than tabs and most programming editors can be set up for this.
I would also suggest getting hold of pylint and using it. It will help you spot a lot of potential errors and will help you to develop good habits.
Its because of the indentation of third_scenario_code() you need to write it under the print .
change the following :
if next == 'Examine the door':
print "A small panel comes out, requires to enter a code of words"
third_scenario_code()
to :
if next == 'Examine the door':
print "A small panel comes out, requires to enter a code of words"
third_scenario_code()

python identation error (2.7.5)

im new in programming and i just started learning python (2.7.5) and im facing this indentation error, i've tried to change the position but still the same , and in the options is set automatically to 4 spaces. what should i do?
if temperature>60 and temperature<75:
print "just right!"
else:
File "<pyshell#8>", line 3
else:
^
IndentationError: unindent does not match any outer indentation level
you have to indent everything after the if line:
if temperature>60 and temperature<75:
print "just right!"
else:
print "not right!"
make sure that all the indents are of the same space width (standard is 4 spaces as you said).
Indentation errors happen when the indentation is of different width like so:
if temperature>60 and temperature<75:
print "just right!"
else:
print "not right!"
Note that no indentation is also an indentation level, like the if and the else in the above example.
As Serbitar said, indentation is extremely important in Python. As you don't use semi-colons like in other languages, Python pays particular attention to indentation.
It also encourages clean code. There are a few situations where indentation isn't important.
Eg:
class ClassName:
def method_name():
print("I am a really long string," \
"So where you indent the second line doesn't"\
"matter")
This also applies to method calls. If you are calling a method where the arguments are particularly long, it doesn't matter where you format the second line.
However, it's always recommended to format appropriately.
If you use the community edition of PyCharm, it helps a great deal with this issue.

TypeError: 'str' object is not callable

I'm very new to Python and I was wondering if anyone could help me with this code I wrote:
print("What is your name:")
name = raw_input()
print "hi", name, "How are you:"
Feeling = raw_input()
if Feeling ("good"):
print ("Good")
Else
print ("Thats a shame")
It just comes up with error this when I run it:
TypeError: 'str' object is not callable
I have looked online but I couldn't find anything that would help because the answers were to complex.
Could someone please help me.
You are trying to use the string stored in Feeling as a function.
if Feeling ("good"):
With nothing but spaces between Feeling and the parenthesis around "good", Python sees that as a function call.
You probably wanted to test if it was equal to "good" instead:
if Feeling == "good":
print "Good"
In this line:
if Feeling ("good"):
You're trying to call Feeling as a function, with "good" as an argument. That's what parentheses mean in this syntax.
If you want to check whether Feeling is equal to "good", you use the == operator:
if Feeling == "good":
Meanwhile, as soon as you fix that, you're going to have another problem with the next line:
Else
In Python, capitalization counts; else has to be spelled in all lowercase. Also, block statements (like if and else) always need colons. And the statements inside a block statement have to be indented. So, you want this:
else:
print("Thats a shame")
You may want to find a smarter editor that helps you with some of this stuff, instead of using Notepad/TextEdit/etc. Any decent programmer's editor—including free ones like SciTE or emacs—will try to put the cursor at the right place for indentation, highlight when you've made a syntax error, and so on.
The problem is this line:
if Feeling ("good"):
Feeling is a string, the result of raw_input(). Feeling("good") looks like you want to call it as if it were a function. That's not possible, so it says "str object is not callable".
You probably meant
if Feeling == "good":
You're also missing a : after the else, you shouldn't capitalize else, and the line after should be indented.

Suggestions on improving my code.

Hello I am a fairly novice programmer. And I have a simple code
name=raw_input("Hello I am Bob. What is your name?")
print("It is very nice to meet you,", name)
response=raw_input("what do you want to do today?")
if response == "price match":
However on the fourth line I get SyntaxError: unexpected EOF while parsing error and I did look into it and I found that using the raw_input for inputted strings is much better than using the input function.I don't know why the error keeps popping up. Could I get some help and perhaps some suggestions as to how I can improve the code.
You have to do something in the if statement. For example, print a price:
if response == "price match":
print "Yes, we can do that for you".
But, you can't just leave a block (the stuff that is indented after a :) empty, or Python will give you an error.
In rare cases (and not in your case here), you may want to do absolutely nothing, but still have a block (e.g. if required to by an except:). In that case, you still have to put something in the block, but you can use the word pass which does nothing. (This is almost never used in an if block).
I'm no Python expert.. But it looks like your problem is here:
response=raw_input("what do you want to do today?") if response == "price match":
You are defining an if statement, but if this value is true, how do you handle it?
I'm guessing you would need something like:
if response == "price match":
print('Match')
else:
print('Did not match')
Hope this helps a little
If you need a placeholder for just having valid syntax before putting in the body of your if/else/functions/etc, use pass.
http://docs.python.org/release/2.5.2/ref/pass.html
As for your code, a valid if statement must always have a body. So put some code there or use pass.

Categories