indentation

Python NameError: name 'self' is not defined Why?

Python NameError: name 'self' is not defined Why? Question: At the top: import pygame, sys from pygame.sprite import Sprite from pygame.locals import * pygame.init() Part not working: class DetectionBox(Sprite): def __init__(self): Sprite.__init__(self) self.img = pygame.Surface([SCREEN_WIDTH, SCREEN_HEIGHT/4], SRCALPHA, 32).convert_alpha() self.pos = (0, SCREEN_HEIGHT – (SCREEN_HEIGHT/4)*3) DETECT_BOX = DetectionBox() Error: NameError: name ‘self’ is not defined Someone …

Total answers: 1

Python IndentationError – Codecademy: Python basics: Pyg Latin 9/12

Python IndentationError – Codecademy: Python basics: Pyg Latin 9/12 Question: Here’s my code: pyg = ‘ay’ original = raw_input(‘Enter a word:’) word = original.lower() first = word[0] if len(original) > 0 and original.isalpha(): if first in ‘aeiou’: print ‘vowel’ else: print “consonant” else: print “empty” and here’s the confusing error message: File “python”, line 10 …

Total answers: 3

Python: Indent all lines of a string except first while preserving linebreaks?

Python: Indent all lines of a string except first while preserving linebreaks? Question: I want to indent all lines of a multi-line string except the first, without wrapping the text. For example, I want to turn: A very very very very very very very very very very very very very very very very long mutiline …

Total answers: 4

Python command line: ignore indentation

Python command line: ignore indentation Question: I am new to Python. In short: During scripting I continuously want to test small bits and pieces of my programs by copying/pasting some line(s) of code from my text editor to the command line Python interpreter. When these lines are indented (for example because they are part of …

Total answers: 4

Custom indent width for BeautifulSoup .prettify()

Custom indent width for BeautifulSoup .prettify() Question: Is there any way to define custom indent width for .prettify() function? From what I can get from it’s source – def prettify(self, encoding=None, formatter=”minimal”): if encoding is None: return self.decode(True, formatter=formatter) else: return self.encode(encoding, True, formatter=formatter) There is no way to specify indent width. I think it’s …

Total answers: 4

How to implement custom indentation when pretty-printing with the JSON module?

How to implement custom indentation when pretty-printing with the JSON module? Question: So I’m using Python 2.7, using the json module to encode the following data structure: ‘layer1’: { ‘layer2’: { ‘layer3_1’: [ long_list_of_stuff ], ‘layer3_2’: ‘string’ } } My problem is that I’m printing everything out using pretty printing, as follows: json.dumps(data_structure, indent=2) Which …

Total answers: 11

Does Python have a built-in function for unindenting a multiline string?

Does Python have a built-in function for unindenting a multiline string? Question: Say I have the string s = “”” Controller = require ‘controller’ class foo view: ‘baz’ class: ‘bar’ constructor: -> Controller.mix @ “”” Every line in the string now has a global 4 space indentation. If this string was declared inside a function, …

Total answers: 4

IndentationError: unexpected unindent WHY?

IndentationError: unexpected unindent WHY? Question: IndentationError: unexpected unindent WHY??? #!/usr/bin/python import sys class Seq: def __init__(self, id, adnseq, colen): self.id = id self.dna = adnseq self.cdnlen = colen self.prot = “” def __str__(self): return “>%sn%sn” % (self.id, self.prot) def translate(self, transtable): self.prot = “” for i in range(0,len(self.dna),self.cdnlen): codon = self.dna[i:i+self.cdnlen] aa = transtable[codon] self.prot …

Total answers: 4

Is there a quick way to decrease the indentation of multiple lines in Python?

Is there a quick way to decrease the indentation of multiple lines in Python? Question: I am a newbie to python programming. I find that decreasing the indentation of a block of codes in python is quite annoying. For example, given the following code snippet for i in range(density): if i < 5: x, y …

Total answers: 4

Python IndentationError: unexpected indent

Python IndentationError: unexpected indent Question: Here is my code … I am getting indentation error but i don’t know why it occurs. -> # loop while d <= end_date: # print d.strftime(“%Y%m%d”) fecha = d.strftime(“%Y%m%d”) # set url url = ‘http://www.wpemergencia.omie.es//datosPub/marginalpdbc/marginalpdbc_’ + fecha + ‘.1’ # Descargamos fichero response = urllib2.urlopen(url) # Abrimos fichero output …

Total answers: 5