简体   繁体   English

Python缩进问题不是由于空白不当引起的

[英]Python indention issue that isn't due to improper whitespace

I'm getting an indention error in the following code: 我在以下代码中出现缩进错误:

 28 def dropletExistsInSummaries( droplet ):                                                                                                                             
 29   """                                                                                                                                                                
 30   dropletExists() -- checks to see if a droplet's URL exists in the summary table, takes one argument                                                                
 31   * droplet is the DID of the raindrop                                                                                                                               
 32   """                                                                                                                                                                
 33   log.debug( "entering" )                                                                                                                                            
 34   c2 = db.cursor()                                                                                                                                                   
 35   d = ( droplet, )                                                                                                                                                   
 36   did = 0                                                                                                                                                            
 37   try:                                                                                                                                                               
 38     c2.execute( 'SELECT did from summaries where did = ?', d )                                                                                                       
 39     did = c2.fetchone()[0]                                                                                                                                           
 40   except Exception, e:                                                                                                                                               
 41     log.error( "dropletExists query failed: %s", e )                                                                                                                 
 42   if did > 0:                                                                                                                                                        
 43     return did                                                                                                                                                       
 44   else:                                                                                                                                                              
 45     return 0                                                                                                                                                         
 46                                                                                                                                                                      
 47                                                                                                                                                                      
 48 def summarizeDroplet( did, url ):                                                                                                                                    
 49   """                                                                                                                                                                
 50   TODO: document this                                                                                                                                                
:x                                                                                                                                                                      
sodaphish@svr-lnx02:~/semanticrss$ ./tst.py 
  File "./tst.py", line 37
    try: 
    ^
IndentationError: unexpected indent

...which doesn't make any sense to me because I've checked to make sure everything is properly indented! ...这对我来说没有任何意义,因为我已经检查过所有内容是否已正确缩进! I've stared at this for hours and am resigned to the fact that I need have help. 我盯着这个看了好几个小时,对我需要帮助的事实深感失望。

To be completely honest, I'm not entirely sure why I had to surround "droplet" with parens at line 35, and my guess is that's what's causing the problem, but I can't tell you with certainty that the issue is related to that. 老实说,我不完全确定为什么必须在第35行用括号包围“小滴”,而我的猜测是造成此问题的原因,但是我无法确定地告诉您该问题与那。

For the record, I'm using Python 2.7.9. 作为记录,我使用的是Python 2.7.9。

Thanks in advance for any help! 在此先感谢您的帮助!

Your function indent is three spaces, while the indentation of your try...except block is two spaces. 您的函数缩进为三个空格,而try...except块的缩进为两个空格。 Fix this and it should work. 解决此问题,它应该可以工作。

PEP 8 recommends four spaces for indenting code, though. 不过,PEP 8建议使用四个空格来缩进代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM