Python 2.7 IndentationError [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I am getting an IndentationError when trying to run my program in a Python Interpreter:
line 127
global map
^
IndentationError: expected an indented block
I am using python version 2.7
What's wrong with the following code?:
def make_map():
global map

Python expects 4 spaces or a tab to indent and align code - similar to Java expecting curly {} brackets are the start of a loop, method or class etc.
def some_function():
somecode
morecode
...
should be formatted as
def some_function():
somecode
morecode
...
It appears that your code throws an exception on line 127, so check this and indent the code as required.
def some_code():
for i in range(1, some_value):
some_method()
if need_more_indent:
indent_code()
do_this_after_indent_code()
this_runs_after_for_loop()
return 'lol'

Related

Python function with excessive number of variables [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 20 days ago.
Improve this question
Very general question: I am attempting to write a fairly complext Python script.
#Part A: 100 lines of python code
#Part B: 500 lines of python code
#Part C: 100 lines of python code
Assume that I want "Part B" taken out of the picture for readability and debugging purposes, because I know that it is running well and I want to focus on the other parts of the code.
I would define a function like this:
def part_b():
#500 lines of python code
#Part A: 100 lines of python code
part_b()
#Part C: 100 lines of python code
The problem with this approach in my case is that there are more than twenty variables that need to be sent to "Part C". The following looks like bad practice.
def part_b():
global var1
global var2
global var3...
I am aware that I could return an object with more than twenty attibutes, but that would increase complexity and decrease readability.
In other words, is there a pythonic way of saying "execute this block of code, but move it away from the code that I am currently focusing on". This is for a Selenium automation project.
Sounds like what you're looking for is modules. Move part B into a separate file, and then import it.
import part_B
# part A here
part_B.run()
# part C here
# to access things that part B does:
do_part_C_stuff(part_B.something)

Is there a way to display a seaborn plot without using the python console? [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 quite new to Seaborn, and I am in the process of learning it. However, when I try to run my code, the plot never shows up and instead, the program ends.
This is my code:
import matplotlib.pyplot as plt
import seaborn as sns
import warnings as wrn
from np.Importing_data import stats
plt.rcParams(['figure.figsize']l = 8, 4
wrn.filterwarnings('ignore')
sns.distplot(stats['InternetUsers'], bins=30)
# sns.boxplot(data=stats, x='IncomeGroup', y='BirthRate')
plt.show(sns)
The 4th line of code is importing the data. This is what I get when running the code normally. The error appears in the Python console, but I do not think this is causing the plot not to be shown.
Traceback (most recent call last):
File "C:/Users/Ratnes.Ratnes-LT/PycharmProjects/demo-rs/np/Seaborn.py", line 11, in <module>
plt.show(sns)
File "C:\Users\Ratnes.Ratnes-LT\PycharmProjects\demo-rs\venv\lib\site-packages\matplotlib\pyplot.py", line 354, in show
return _backend_mod.show(*args, **kwargs)
TypeError: show() takes 1 positional argument but 2 were given
Process finished with exit code 1
If I try and run this on the python console, the code works as expected.
This is the result.
The matplotlib plots do show up but the seaborn ones don't. If anyone's got any solutions, please post on here.
Please read the document of matplotlib.pyplot.show. The only argument that can be passed as an argument is a bool value which indicates whether you need interactive or non interactive mode (default value is None), which is basically what your error says as well.

Python Function Won't Run At ALL? [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
I am fairly new to the programming world and am wondering why the following code will not only refuse to run, but my python software won't even give me any error messages whatsoever.I'm using Pythonista, an IOS app for python. I can't get the app to run this code (and it won't give me any error messages) and was wondering if it's my code itself, or is it just the application. Any knowledge on this issue would be greatly appreciated.
def starBits():
badMatchups = [Zelda, Cloud, Ryu]
worstMatchups = [Jigglypuff, Villager, Bayonetta]
print(badMatchups)[1:2]
print(worstMatchups)[1:1]
def main():
starBits()
main()
I'm not sure what you expect from this, but it's really funky syntax.
print(badMatchups)[1:2]
print(worstMatchups)[1:1]
If those slices are subscripts for the lists, you need them inside the call to print:
print(badMatchups[1:2])
print(worstMatchups[1:1])
By the way, do you realize that [1:1] is an empty slice? The second number is the first position not included. You may need
print(badMatchups[1:3]) # two elements
print(worstMatchups[1:2]) # one element
Also, are those elements external variables, or are they supposed to be literal names? If the latter, then you have to put them in quotation marks.
badMatchups = ["Zelda", "Cloud", "Ryu"]
worstMatchups = ["Jigglypuff", "Villager", "Bayonetta"]
With this change, the code runs; I hope it's what you want.
Can't get it to run? Reality check time ...
Full code, changes made:
def starBits():
badMatchups = ["Zelda", "Cloud", "Ryu"]
worstMatchups = ["Jigglypuff", "Villager", "Bayonetta"]
print(badMatchups[1:3])
print(worstMatchups[1:2])
def main():
starBits()
main()
Output:
['Cloud', 'Ryu']
['Villager']

Class name not defined when instantiating [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
Learning Python, please go easy
I have removed all other methods defined in this class, here is the skeleton of whats left :
from random import randint
class CreditCardNumberGenerator:
def __init__(self):
print "Hello World"
pass
if __name__ == "__main__":
o = CreditCardNumberGenerator()
Error
Traceback (most recent call last):
File "del.py", line 3, in <module>
class CreditCardNumberGenerator:
File "del.py", line 11, in CreditCardNumberGenerator
o = CreditCardNumberGenerator()
NameError: name 'CreditCardNumberGenerator' is not defined
i have checked name, typecase and all possible SO thread, no help....can some one please advice??
I am pretty sure, its something very obvious which i am missing here!!! :\
if __name__ == "__main__":
o = CreditCardNumberGenerator()
is too much indented - make it aligned to the same column as class and it should be OK.

another Python indentation issue [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
for reason, now my code has some weird formatting issue.
if __name__ == "__main__":
import doctest
# ### UNCOMMENT THESE ONE AT A TIME TO GET THE TESTS TO PASS.
# ### WHEN YOU ARE FINISHED, LEAVE ONLY THE "allTests.txt" line uncommented.
# doctest.testfile("noConsecDupsTests.txt", verbose=True)
# doctest.testfile("isVowelTests.txt", verbose=True)
# doctest.testfile("countVowelsTests.txt", verbose=True)
# doctest.testfile("allVowelsATests.txt", verbose=True)
# doctest.testfile("syllableHelperTests.txt", verbose=True)
# doctest.testfile("removeSilentETests.txt", verbose=True)
# doctest.testfile("removeEdWhenNotASyllableTests.txt", verbose=True)
# doctest.testfile("countSyllablesTests.txt", verbose=True)
doctest.testfile("allTests.txt", verbose=True)
Python 3.3.2 is saying that the indentation error is in the first line of the code... I'm really confused. I've never been so stumped on such a simple topic.
I would guess that you are trying to run this in the Python REPL? As opposed to saving it to a file and then running it?
Python REPL ends a code block after an empty line. Try saving this in a file, say test.py and then call it from command line using python test.py.

Categories