I have an unindent error while using geany with python [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 7 years ago.
Improve this question
I'm using geany and I get the following error
File "autoblog2.py", line 9
htmlfile = urllib.urlopen(url)
^
IndentationError: unindent does not match any outer indentation level
here is my code.
import urllib
import re
symbols_list = ["aapl","spy","goog","nflx"]
i = 0
while i<len(symbols_list):
url = 'https://uk.finance.yahoo.com/q?s='+symbols_list[i]
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
regex = '<span id="yfs_l84_aapl">(.+?)'+symbols_list[i]'</span>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)
print 'the price of' +symbols_list[i]
i+=1
I don't get any errors when I run the same code on a single url. I've only had since trying it with a while loop, i'm using python 2

This can happen when you edit one script with two editors.
Your indent settings can differ from editor to editor.
Take a look at the script with another editor.
If the script has the same indents in other editors the only way is to remove all indents and add them again.
I would recommend the python-idle.
It should show the indents like the interpreter reads them.
Good Luck.

Related

Why is python giving me an error for improper indentation when my indentation is correct? [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 2 years ago.
Improve this question
This is the error message I got:
File "main.py", line 15
while True:
^
IndentationError: unindent does not match any outer indentation level
This is my full code:
import wikipedia
from colorama import Fore, Style, Back
y = input("tell me what you want ")
z = int(input("how many sentences "))
try:
text = wikipedia.summary(y, sentences=z)
print('')
print("---Text---")
print(text)
print("----------")
print(len(text.split()),"words.")
except:
print(Fore.RED + "ERROR)
while True:
print("\a")
Can you please explain why this is happening? I am using the Repl online ide.
The answer to this is that I was mixing up tabs and spaces. You shouldn't use both because this error can happen.
While coding in python, it's super important to pay attention to the way you indent. Of course you can either use tab or space, but always make sure you stick with one of them and not mixing them together.
Seems like you haven't done it this time. Delete the indentations and reindent them. Should work fine.

i don't know how to fix this error, python3 syntax error 'def' [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 3 years ago.
Improve this question
I want to solve this error.
i tried for almost half hours, but I couldn't find the answer..
This is my error
File "sampling_fun.py", line 71
def average(self) :
^
SyntaxError: invalid syntax
and full code
import csv
class fun :
def __init__(self, rowList, num) :
list = []
listLen = len(self.rowlist)-1
for i in range(listLen) :
list.append(self.rowList[i+1][num] # num = Header 1~4
def average(self) :
ave = sum(self.list)/self.lestLen
print("average : %0.2f" %ave)
return ave
testlist = cssRead('Data_2', 1)
test1 = fun(testlist, 1)
test1.average()
When you encounter such error, check the line before it.
You have a non-matching paranthesis, the closing brace for append is missing.
list.append(self.rowList[i+1][num])
def errors happen when python is not expecting a function definition. As #Siong Thye Goh notes, a missing ) is your issue. For the future, the only time python does not expect a function definition is after an incomplete code-block.
That can happen when you forget to close parentheses or brackets, but also when you forget to put a statement after a colon or don't indent correctly.
Rarely, it can happen due to incompatible space characters when copy-pasting from external sources.

Indentation Error with python: Indented block [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
>>> for link in soup.find_all('a'):
... print link
File "<stdin>", line 2
print link
^
IndentationError: expected an indented block
The correct indentation should be:
for link in soup.find_all('a'):
print(link)
Try this snippet to understand the need for indentation
for x in range(3):
print("Inside the loop", x)
print("Outside the loop, this print is run only once")
This notion is well explained in the beginning of Python tutorial:
The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.

finding HTTPS images with BeautifulSoup, 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 4 years ago.
Improve this question
I'm iterating through all the img's in a request.POST to see if they are HTTPS (I'm using Beautiful Soup to help)
Here's my code:
content = request.POST['content']
print(content) #prints:
<p>test test test</p><br><p><img src="https://www.treefrogfarm.com/store/images/source/IFE_A-K/ClarySage2.jpg" alt=""></p><br><p>2nd 2nd</p><br><p><img src="https://www.treefrogfarm.com/store/images/source/IFE_A-K/ClarySage2.jpg" alt=""></p>
soup = BeautifulSoup(content, 'html.parser')
for image in soup.find_all('img'):
print('Source:', image.get('src')[:8]) #prints Source: https://
if image.get('src')[:7] == "https://":
print('HTTPS')
else:
print('Not HTTPS')
Even though image.get('src')[:7] == "https://", the code still prints Not HTTPS.
Any idea why?
Well for starters, 'https://' is 8 characters, so there's no way that a slice of 7 characters can match it.
Also, please make your question titles actually indicative of the problem you're having rather than unrelated accusations about the python operators.
to match the https:// string the appropriate slice would be :8 instead of :7
if image.get('src')[:8] == "https://":

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.

Categories