运行时不断告诉我:
期望一个缩进的块
但是我不希望在我的except块中没有写任何内容,我只是希望它能够捕获并吞下异常.
我决定,我学习了一点Python.第一个介绍说它使用缩进来分组语句.虽然最好的习惯显然只使用其中一种,如果我互换它会发生什么?多少个空格将被视为等于一个标签?如果标签和空格混合在一起会不会起作用?
首先让我说我完全是编程的新手.我刚刚收到了Python并且它一直在我的脑海中引发一个反复出现的错误 - "预计会有一个缩进的块"现在,我知道还有其他一些线程解决了这个问题,我已经查看了很多这些问题然而,即使检查我的缩进也没有给我更好的结果.我用4个空格替换了所有的缩进,甚至重写了几次代码.我将以此为例发布此计数器作业.
option == 1
while option != 0:
print "MENU"
option = input()
print "please make a selection"
print "1. count"
print "0. quit"
if option == 1:
while option != 0:
print "1. count up"
print "2. count down"
print "0. go back"
if option == 1:
print "please enter a number"
for x in range(1, x, 1):
print x
elif option == 2:
print "please enter a number"
for x in range(x, 1, 1):
elif option …Run Code Online (Sandbox Code Playgroud) 这是我的代码...我得到缩进错误但我不知道为什么会发生.
- >
# 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 = open(fname,'wb')
# Escribimos fichero
output.write(response.read())
# Cerramos y guardamos fichero
output.close()
# fecha++
d += delta
Run Code Online (Sandbox Code Playgroud) 我只是无法弄清楚这有什么问题......
#!/usr/bin/env python
#
# Bugs.py
#
from __future__ import division
# No Module!
if __name__ != '__main__':
print "Bugs.py is not meant to be a module"
exit()
# App
import pygame, sys, random, math
pygame.init()
# Configuration Vars
conf = {
"start_energy": 50,
"food_energy": 25,
"mate_minenergy": 50,
"mate_useenergy": 35,
"lifespan": 300000
}
class Bugs:
def __init__(self):
self.list = []
self.timers= {}
# Names / colors for sexes
self.sex = ["Male", "Female"]
self.color = ["#CBCB25", "#A52A2A"]
# Bug info tracking …Run Code Online (Sandbox Code Playgroud) IndentationError:意外的unindent为什么???
#!/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 ">%s\n%s\n" % (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 += aa
def parseCommandOptions(cmdargs):
tfname = cmdargs[1]
sfname = cmdargs[2]
return (tfname, sfname)
def readTTable(fname):
try:
ttable = {}
cdnlen = -1
tfile = open(fname, "r")
for line in tfile:
linearr = line.split() …Run Code Online (Sandbox Code Playgroud) 我是Python新手,我收到此错误:
Traceback (most recent call last):
File "/usr/local/bin/scrapy", line 4, in <module>
execute()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scrapy/cmdline.py", line 130, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scrapy/cmdline.py", line 96, in _run_print_help
func(*a, **kw)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scrapy/cmdline.py", line 136, in _run_command
cmd.run(args, opts)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scrapy/commands/crawl.py", line 42, in run
q = self.crawler.queue
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scrapy/command.py", line 31, in crawler
self._crawler.configure()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scrapy/crawler.py", line 36, in configure
self.spiders = spman_cls.from_settings(self.settings)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scrapy/spidermanager.py", line 33, in from_settings
return cls(settings.getlist('SPIDER_MODULES'))
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scrapy/spidermanager.py", line 23, in __init__
for …Run Code Online (Sandbox Code Playgroud) 我无法理解为什么python会出现"预期的缩进块"错误?
""" This module prints all the items within a list"""
def print_lol(the_list):
""" The following for loop iterates over every item in the list and checks whether
the list item is another list or not. in case the list item is another list it recalls the function else it prints the ist item"""
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item)
else:
print(each_item)
Run Code Online (Sandbox Code Playgroud) 这是代码:
def myfirst_yoursecond(p,q):
a = p.find(" ")
b = q.find(" ")
str_p = p[0:a]
str_q = p[b+1:]
if str_p == str_q:
result = True
else:
result = False
return result
Run Code Online (Sandbox Code Playgroud)
这是错误:
Traceback (most recent call last):
File "vm_main.py", line 26, in <module>
import main
File "/tmp/vmuser_ssgopfskde/main.py", line 22
result = False
^
IndentationError: expected an indented block
Run Code Online (Sandbox Code Playgroud)
我的代码出了什么问题?
这段代码,test.py:
if 1:
print "foo"
print "bar"
Run Code Online (Sandbox Code Playgroud)
可以使用execfile("test.py")或成功执行python test.py,但是当一个人尝试将其复制粘贴到python解释器中时:
File "<stdin>", line 3
print "bar"
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
为什么会这样?可以通过配置解释器以便成功读取复制粘贴的文本吗?我想这可能会影响口译员的输入,但这对我来说没问题.