I'm trying to make a game in python, and stack trace keeps coming up with 'invalid syntax'(those are the EXACT words of the stack trace, not me summing it up) at this line:
print('Look(1) | Bust down door with weapon(2)')
The cursor is always between the 'p' and the 'o' whenever I check it with ALT+X, regardless of where it
was before I checked it.
There are no syntax errors in that line that I can recognize. I was wondering if someone more experienced could help me?
These are the lines above and below that in case you think it's not that line itself that's causing the
problem:
print('You are in a dark and grimy dungeon. You see no windows, and a door. What\
would you like to do?')
print('Look(1) | Bust down door with weapon(2)')
act_umpteenth = input('>>> ')
while act_umpteenth:
[rest of code continues on here that I'm not bothered to copy and paste]
Edit: Solution found. The culprit was a close bracket behind a string that I was assigning to a variable a few lines up. I think I thought it was a print statement or something. :P I deleted it, and now the code works 100%. Thanks for suggesting that I look over the code for indentation/tab/spaces problems, even if it wasn't the problem, because otherwise I wouldn't have spotted the actual problem.
I would just go and redo all your tabs. You can highlight all the code, and press Shift + Tab (you might have to do this multiple times) to back everything up to the left edge. Then you'll want to re-tab everything over in the proper way.
try running it with
python -tt myscript.py
you are probably mixing tabs and spaces when you run it with the -tt command it will tell you about inconsistent indentation
fixing your indentation should resolve your issue
I figured I would put this as an answer even though it is really a comment
Related
Update: The original question was posted using C-RET to run the lines, which produces the error. I get no such error using The menu Python > Eval region, which successfully evaluates the script.
However, using the menu for every execution is really annoying, and moreover I can't run the present line alone with that method. So my problem remains.
Forgive me if I'm missing something obvious, but I can't figure this out.
This code is giving me an IndentationError. I can't imagine what's going on.
for x in range(0, 3):
print "We're on time %d" % (x)
Here's everything related to python in my emacs.el
;; elpy
(add-to-list 'package-archives
'("elpy" . "http://jorgenschaefer.github.io/packages/"))
(package-initialize)
(elpy-enable)
You've mixed tabs and spaces. This can lead to some confusing errors.
I'd suggest using only tabs or only spaces for indentation.
Using only spaces is generally the easier choice. Most editors have an option for automatically converting tabs to spaces. If your editor has this option, turn it on.
You can paste your code in notepad to see.
All commands from menu have a command-name callable via M-x COMMAND RET.
In this case M-x python-send-region RET
I am trying fix some indentation errors in a Python script. Is there a way to auto correct the errors online or using other utilities?
I hope this error is very familiar, but wanted to avoid this again. Does any editor help in fixing these issues?
IndentationError: expected an indented block
It's not possible in general because some cases would be ambiguous:
something = foo()
if something:
something.bar()
print("ok") # Only half indented. Should this be inside or outside of the if statement?
It's much better to find an editor with auto-indentation support and make sure you indent properly (even in languages without significant whitespaces, as it makes your code easier to read).
You can use reindent.py
For me it worked on some files but it didin't work on others.Give it a try.
reindent.py
Uasge:
reindent.py <filename>.py
Change Python (.py) files to use 4-space indents and no hard tab characters.
Also trim excess spaces and tabs from ends of lines, and remove empty lines
at the end of files.
You can use Spyder IDE
Source Menu -> Fix Indentation
This particular error usually occurs when you forget to add an indented block after a statement that needs one (i.e. if or for). You likely forgot to indent the required block or accidentally hit backspace (or something) such as
def sayhello():
print "Hello world!"
Sometimes you don't want any code after an if statement or after try/except statements, but python will require at least one line after them, so you can use the pass keyword
try:
x= 4/0
except:
pass
print x
OK, here's a piece of the script:
def start():
print "While exploring the ruins of a recently abandoned castle you stumble apon the entrance to what appears to be a dungeon. You are carrying on you a...
I keep getting the error
user#ubuntu:~/Documents/python$ python dungeon.py
File "dungeon.py", line 533
def start():
^
IndentationError: expected an indented block
I know this is probably obvious but does anyone have any clue as to what I'm doing wrong hear? I tried replacing the indent
with spaces only and tabs only but it still gives me this error. I appreciate any answers.
Make sure you aren't mixing tabs and spaces for indentation
Your string is missing a closing quote
Further, is def start() actually indented in your file? It isn't indented in this question, and that's what Python seems to be complaining about.
Looks like it expects def start(): to be indented. What does the code look like before that?
Check that whitespace is consistent (i.e. mixing tabs and spaces?).
If your editor supports, I suggest to make it expand all tabs to (4) spaces. This avoids such confusion, also when copy-pasting code.
In vim:
:se tabstop=4 shiftwidth=4 expandtab
:%retab
Are you sure there is a tab character in that position? Try open it with vi and add yourself the tab just to check is fine.
Try looking at the code just before that line. Is there an unfinished block (line ending with :)? If yes, put something in that block – a pass statement will do.
I added a print line to a python script while the script was executing, and now all the text is highlighted in red when I open the file. Opening and closing the file doesn't get rid of it. Opening a second python file momentarily fixed the problem, but then closing file and reopening brought the problem back. Now it wont go away at all. Any body know what could cause this?
This happens sometimes in vim when it goes to highlight syntax on multi-line comments. Occasionally everything after the multi-line comment also becomes colored the same as the comment.
I am unsure whether this is a legit bug in vim, or in the actual syntax settings for Python (e.g. python.vim), but I definitely experience this from time-to-time.
For a quick fix, you might try typing:
:hi Error NONE
And then hit Enter.
Old thread, but hope this helps.
By mistake I did a "/." on my vim screen, which highlighted all lines in red. If I open any other file, the red highlighting stays.
Try searching for some other keyword, let's say "/word" - doesn't matter word exists or not. It restores the color.
You probably have an unterminated multiline string. They start and end with either three single or three double quotes.
''' <== this is the start of a multiline string
this is still in the string
this is the last line '''
As per this article http://vim.wikia.com/wiki/Fix_syntax_highlighting, I mapped F12 to resync the syntax highlighting from the start of the file. It has worked better in some instances where <ctrl-l> didn't.
noremap <F12> <Esc>:syntax sync fromstart<CR>
inoremap <F12> <C-o>:syntax sync fromstart<CR>
Right now I'm working on a Tetris Game (sorta, I found a Tetris example for Python on a website, I've been copying it but adding some of my own stuff), and just finished writing all the code but have had a couple syntax errors. I've been able to fix all of them but this last syntax error is confusing to me.
def pieceDropped(self):
for i in range(4):
x = self.curX + self.curPiece.x(i)
y = self.curY - self.curPiece.y(i)
self.setShapeAt(x, y, self.curPiece.shape()
self.removeFullLines()
The specific syntax error is on the last line of the function and I don't understand why, the indentation and whitespace all seems correct. So could someone explain how this is a syntax error?
You didn't close the parenthesis of self.setShapeAt.
There's an extra whitespace on the last line - just ahead self.removeFullLines(). Its indenting is thus not the same as the for line's indenting. EDIT: Seems to be corrected now.
Always use the same indent sequence - choose either tabs, or n whitespaces. But be consistent. Some editors (such as VIM) are able to insert the appropriate number of whitespaces whenever you hit tab.