IndentationError in a Python exercise [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am a beginner so I am sure this is mundane. I am following a beginner's exercise and I typed the code correctly and check and recheck the copy I typed but it doesn't look like what the exercise should look like. Two lines should appear in the output, but only one of them appears. I will include what I type in Atom below and what the code should look like. I am working at following "Learn Python the Hard Way" and this is one of the exercises. I am using a Macbook with Atom as my editor. Notice how only one of the sentences appears in my finished results?
The code I entered in Atom:
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print "I could have code like this." # and the coment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run."
What the code should do if written properly:
$ python ex2.py
I could have code like this.
This will run.
This is what I see when I go to my terminal:
IndentationError: unexpected indent
Renays-MacBook-Pro:mystuff3 renayjohnson$ python ex2.py
File "ex2.py", line 9
print "This will run."
^
IndentationError: unexpected indent
Renays-MacBook-Pro:mystuff3 renayjohnson$

At line 9, the print line is indented with a space. Just remove that space character and rerun your script. Also, looking at the original source of your exercise, there is no indentation in that line. It's worth-mentioning that comment lines are indented at the same level of the code that follows them. In your code at line 7, the block comment line shouldn't be indented as well but it isn't raising any error in that comment line.
However, you should read about the Off-side rule and how such programming languages are expressed by their indentation.

Related

Python, unexpected EOF while parsing error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am a complete beginner in Python, having used R quite a lot before.
I am trying to write a multiline string which is very straightforward in R but I receive an error that stumps me, I have googled around everywhere but the suggested solutions have not worked.
When I try to use parentheses as I have seen suggested:
multiline = ("Hello,"
" my name is"
" James")
I receive:
multiline = ("Hello,"
File "<ipython-input-56-f67f7efad636>", line 1
multiline = ("Hello,"
^
SyntaxError: unexpected EOF while parsing
Similarly I tried triple quotes, also suggested:
multiline = """Hello,
my name is
James"""
I receive:
multiline = """Hello,
File "<ipython-input-58-0879a928a2ee>", line 1
multiline = """Hello,
^
SyntaxError: EOF while scanning triple-quoted string literal
I am sure I will be missing something blindingly obvious to more experienced Python users but any help would be greatly appreciated.
Thanks very much
Apparently, this is neither an issue with python, ipython or spyder. The mistake is using the incorrect command to run your code. You probably used Run Selection or Current Line, while the cursor was in the first line of the multiline strings. This command doesn't consider the context, so it essentially pastes the line into the interpreter and runs. Your first error was caused by python not finding the closing parentheses. Your second error is python not finding the closing triple quotes. In spyder, one typically makes code cells with #%% and runs them with CTRL+ENTER or SHIFT+ENTER. That should fix your problem.

Weird tab error, can't put my finger on where I went wrong [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to make use of a while loop to iterate through a list of three variables that's are also assigned to three queues implemented through linked lists. I called this list full. My while loop is supposed to keep running while my list of full queues is still at 3.
Here's the chunk of code. I get the tab error after the pop function.
while len(full) == 3:
x = random.random()
if 0 <= x <.33:
if full[0].isEmpty() == True:
full.pop(0)
else:
Runway.enqueue(airplane)
You have at least one tab character in your code at the start of your else: line. (There are two tabs in this editor--there may be only one in your code.) The other lines use spaces.
In Python it is a very good idea to use only spaces in your code and never use tabs. It is possible to use some tabs and get away with it, but getting away with it is very unlikely. Set your code editor to insert spaces when you press the Tab key--all good editors have a setting for that.
In the code you show us, the line after the else: is not indented at all, so that should also give you an error. Indent that line by four spaces, after you replace the tabs with spaces in the else: line.
You are getting a tab error because you're mixing tabs and spaces in your indentation.
I copy pasted your code and the "else" line starts with a tab instead of 4 spaces. Try changing it to 4 spaces and see if the problem is fixed.

Indentation error shows even correct indentation in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
See below is my script, i already indented the code using spaces but still it shows error
def main(username, password):
if(username and password):
if os.path.isfile("macid.txt"):
num_lines = sum(1 for line in open('macid.txt'))
if (num_lines > 0):
print "Number of lines: ", num_lines
else:
print "macid.txt file is empty!"
else:
print "macid.txt file not found!"
else:
print "error"
if __name__ == '__main__':
main("example", "password")
The error is as shown
num_lines = sum(1 for line in open('macid.txt'))
^
IndentationError: expected an indented block
Glad it could help! Following your comment, I'll post this as an answer.
Which program do you use for editing? If I recall correctly, back in University my classmates learned Python (and I C) and I remember them saying that there were an issue with their editor (I believe Notepad++) where they had to put 4 or so spaces as a substitution to tabs. Also if you run Notepad++ can you use the "Find" option ("Search" in ribbon -> Find) and press "Extended" under "Search mode" and then search for "\t", i.e. tabs and see if you in fact have mixed spaces in some places, and tabs in others which I think can cause these types of problems

re.findall working in console but not in script? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm probably missing something very basic here, but here goes:
I'm using Python 2.7 and regex to identify digits within a string.
In the console, I type in:
>>> newstr = 'NukeNews/File_132.txt'
>>> int(re.findall(r'\d+',newstr)[0])
132
Which is what I expect.
However, in the script I'm running, I have the strings stored in a dictionary, linedict. I'm running this script:
news_id=[]
for line in line_vec:
print linedict[line]
newstr= linedict[line]
id_int = re.findall('r\d+',newstr)
print id_int
news_id.append(id_int)
It's a long list, but the output looks like:
NukeNews/File_132.txt
[]
So - the correct string is registered, but it's not matching on anything.
I was calling the first item in the list earlier (to match the console input of int(re.findall(r'\d+',newstr)[0]), but the script is telling me that the regex didn't find any instances of the digits in the string. I would expect this to return:
NukeNews/File_132.txt
['132']
Any idea why it's not working as expected? When I try running re.match(r'/d+',newstr) I also get an empty group (following the groups example on https://docs.python.org/2/library/re.html).
Edit: As pointed out, this is a case of not being careful with 'r' and r'*'. I'm just going to leave this up in case anyone else googling "why does my regex work in console but not in script" forgets to check this typo, like I did.
You've got your r inside the quotes so instead of getting a "raw string" you're getting a string with an 'r' in it ...
id_int = re.findall('r\d+',newstr)
# ^
# should be:
id_int = re.findall(r'\d+',newstr)
your "console" version also only takes the first of the found matches compared to your "script" version which appends the entire list.

python:cant figure out whats wrong in this code but still i am getting an taberror [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
getting an error in line 28 having if count<10 : ^
TabError: inconsistent use of tabs and spaces in indentation.
count= input('Give me no. of donuts: ') if count<10 : print('No. of donuts: %s' % (count))
else : print('No. of donuts:Many')
return
to see how i exactly wrote it, follow the link given
http://pzy.be/v/1321/Untitled.jpg
You have mixed tabs with spaces while indenting your code. Don't do that.
How about reading the error message:
TabError: inconsistent use of tabs and spaces in indentation.
You cannot mix tabs and spaces in Python, because how many spaces a tab counts as is dependent upon what program and what settings you are viewing it in. Whitespace, and in particular how indented a line of code is, is very important in Python as it determines flow control and many other things. So all people must see the same code.'
For example, these are different in Python:
if a:
b()
c()
-
if a:
b()
c()

Categories

Resources