Editor won't accept else in if statement [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Can anyone explain why I am getting a syntax error on the below, my editor won't accept the "else":
for i in self.jobs:
with open('postcodes.csv', 'rb') as f: #see http://docs.python.org/2/library/csv.html#csv-examples
reader = csv.reader(f)
for row in reader:
if row[0] == self.jobs[i][3]:
self.jobs[i].append((row[1],row[2]))
else:
self.jobs[i].append("Latitude & Longitude not available")

Your indentation is inconsistent because it's a mixture of space and tab characters, as you can see in the screenshot below where I've turned on a "visible whitespace" mode in my editor.
I suggest you convert all the tabs in your code to space characters -- perhaps your editor has a command to do that -- and avoid mixing the two in the future. Since, as you're discovering the hard way, it can cause bizarre and hard-to-diagnosis errors.

In the future, consider evaluating your code for PEP-8 deviations:
http://pych.atomidata.com/

Related

inaccurately reading from file logic error Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi I have the following text file on which I am using a csv reader:
number,obstacle,location,message
1,gobacktostart,8,Sorry but you've landed on a dangerous number, back to the start for you!
2,movetosquare42,matrix[1][0],The meaning of life is 42 or so they say, and that's where you're headed
I wish to (at the end) retrieve the number 8 from the row that starts 1,gobacktostart,8....etc.
My code is:
def gobacktostart():
with open("obstacles.txt","r") as f:
idnumber="1"
fReader=csv.reader(f)
for row in fReader:
for field in row:
if field==idnumber:
print(row[3])
player1position==row[2]
print(player1position)
and the undesired output however, is:
>>>
Sorry but you've landed on a dangerous number
1
>>>
I do need to read the value into the variable player1position in order to pass it on to another function at a different part of the program.
Any thoughts on solving this logic error? Why is it printing "1", when row[2] refers to the 8. Also, Row[3] seems to execute properly in the previous line....
You are checking for equality not assignment in player position == row[2]

Indentation error? Python3x [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 6 years ago.
Improve this question
Python3 is telling me I have an error in my indentation. I've tried about a million different indentations and I am lost. It's not pointing to the error directly, and just pointing to a parenthesis, leaving me to figure it out on my own.
import os
for root, dirs, files in os.walk('C:\\Users\\Tom\\Desktop'):
for file in files:
if file.endswith('.txt'):
f = open("test.txt", "r+")
f.seek(0)
for line in f:
a = f.read()
f.seek(0)
for char in a:
o = ord(char)
f.write(str(o))
f.truncate()
Apologies, I forgot to include the error message.
File "C:\Users\Tom\Desktop\Search.py", line 6
f = open("test.txt", "r+")
^
TabError: inconsistent use of tabs and spaces in indentation
I loaded the text from your question into a text editor (vim) and showed invisible characters, which renders this.
Here, spaces show as space, and tab shows as ^I. As you can see, your second for and first if lines are indented with spaces, and the rest of the file is indented with tabs.
In the general sense, this creates a real mess in Python, where indentation is syntactically significant to the program structure.
In Python 3 specifically, mixing tabs and spaces as indentation is a fatal compile error. That is what you've encountered (TabError).
See PEP-8, which suggests using spaces only, never tabs, and using a 4-space indent.

url.strip()+' - CatchAllError' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am try to run this program as it start run but while running it's create error regrading CatchAllError . I want output as ' Pass ' But its not please help. Thanks
if line:
msg = url.strip()+' - CatchAllError'
print msg
with open("log_Error.txt", "a") as log:
log.write(msg+"\n")
else:
pass
You can see my whole program at https://ghostbin.com/paste/ypdmd.
You can see that if condition should be pass but it's not. To include target list go to https://ghostbin.com/paste/pjuox and download target list and other information.
You are mixing tabs and spaces. This confuses Python* and may cause unexpected behavior.
Use only tabs or only spaces, not both. Spaces is preferable.
(*well, not really. Python knows exactly how to handle tabs; it just does so in a way that is very surprising to most users. In particular, one tab is not equivalent to 4 spaces, or 8, or whatever it looks like in your text editor. See 2.1.8 - Indentation for more information.)

Python-It does not let me write to a text file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
myfile.write(Car_Number_plate+str' went at',+ Speed_In_Miles_Per_Hour+'/n')
I am getting an syntax error on python, I am trying to write to a text file.
Yes, you would. Here is valid syntax for what you are trying:
with open("file.txt", "w") as myfile:
myfile.write(Car_Number_plate + ' went at ' + Speed_In_Miles_Per_Hour + '/n')
To make it better, you probably meant to type \n for a line break, not /n and you probably want to make sure those variables are strings or don't use + to combine them. You should also make it more PEP8 friendly. So:
car_number_plate = "ABC123"
speed_in_miles_per_hour = 33
with open("file.txt", "w") as myfile:
myfile.write("{0} went at {1} mph \n".format(car_number_plate, speed_in_miles_per_hour))

How can I add a print statement to this code without getting an indentation error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to python and trying to learn an unfamilar code base. I want to add a print statement just below the def line. But every time I do so for one of these very short methods I get an indentation error.
def rename(self, old, new):
#print 'this will bring an error if left uncommented'
return os.rename(self._full_path(old), self._full_path(new))
How can I add a print statement to a short method like this?
A tab is a tab character (just like \n is a newline character), you can configure most editors to replace a tab character with a certain number of spaces.
The convention in Python is to indent by four spaces and to use spaces instead of tabs. This is the recommendation; but people tend to do what they please.
It is most likely that the code is indented by tabs and you are using spaces. #TimPeters wrote reindent.py that will take a Python file and convert tabs into spaces.
There are other tools as well. Most editors have a function that can do this, if the editor is Python aware they may have a specific function for just this. For example pycharm has a "Convert Indents" menu option under Edit.
Try this code:
def rename(self, old, new):
print('this will bring an error if left uncommented')
return os.rename(self._full_path(old), self._full_path(new))
Whenever I have trouble with the print keyword, I can usually solve it by using print() instead.

Categories