开发者

Python development - unexpected indent problem

开发者 https://www.devze.com 2023-03-12 18:25 出处:网络
I have recently inserted an if statement into some previously working code. I now get an unexpected indent error when I run it on the start of my if statement.

I have recently inserted an if statement into some previously working code. I now get an unexpected indent error when I run it on the start of my if statement.

   ImpressionsBasedOnWeight = setImpressionsBas开发者_高级运维edOnWeight(setImpressionsBasedOnWeightData)
   impressions = data[2]
       if reportSuite.RP_UniqueUsers > weighted_impressions:
           users = weighted_impressions
       else:
           users = reportSuite.RP_UniqueUsers
   Pages_in_thousands = impressions / 1000

If I remove the indent it fails saying it expects an indent and if I then add the indent it fails saying unexpected indent...


impressions = data[2] is a simple assigment. There should be no change of the indentation afterwards. So you need to have impressions = .. and if .. with the same indentation:

ImpressionsBasedOnWeight = setImpressionsBasedOnWeight(setImpressionsBasedOnWeightData)
impressions = data[2]
if reportSuite.RP_UniqueUsers > weighted_impressions:
    users = weighted_impressions
else:
    users = reportSuite.RP_UniqueUsers
Pages_in_thousands = impressions / 1000

Also, make sure you're not switching between tabs and spaces for indentation. You can set some indicators to display symbols for tabs, and a good editor should unify indentation when you select the whole code, press Tab and then Shift+Tab.


Why to put if with indent?

   ImpressionsBasedOnWeight = setImpressionsBasedOnWeight(setImpressionsBasedOnWeightData)
   impressions = data[2]
   if reportSuite.RP_UniqueUsers > weighted_impressions:
       users = weighted_impressions
   else:
       users = reportSuite.RP_UniqueUsers
   Pages_in_thousands = impressions / 1000
0

精彩评论

暂无评论...
验证码 换一张
取 消