简体   繁体   English

语法错误:循环中 else 运算符行的语法无效

[英]An syntaxError: invalid syntax at the line of else operator in the loop

Sorry in advance for posting such a long piece of code, I shortened it as far as possible, but I think that the problem can be connected to the incorrect positioning of lines in a long loop so I see it necessary to illustrate it until the place where the error arises.很抱歉发了这么长的代码,我尽量缩短了,但我认为问题可能与长循环中行的错误定位有关,所以我认为有必要说明它直到地方出现错误的地方。

In the 10th line from the end (marked by "# Error arises here (!)") at the line of the "else" operator, I get an error "SyntaxError: invalid syntax".在从末尾算起的第 10 行(由“#Error 在此处出现 (!)”标记)中,在“else”运算符的行中,出现错误“SyntaxError: invalid syntax”。 My initial intuition was that an error can be connected to improper positioning of the operator, but the text of the error doesn't seem to say so (i would expect "IndentationError: unexpected indent" error then) and it seems to me that "else" operator is positioned correctly.我最初的直觉是,错误可能与操作员的不当定位有关,但错误的文本似乎并没有这么说(那时我预计会出现“IndentationError:意外缩进”错误),在我看来,“ else”运算符的位置正确。

Thank you.谢谢你。

if (Test_set.loc[n, '4_signals'] == 1):

 if (Portfolio_1_4_Date_sell < pd.Timestamp(Test_set.loc[n, 'feedTimestamp'])):

   try: 
        SP1500selldate_2 = n_short
        SP1500sellprice_2 = SP1500DailyReturns.loc[SP1500selldate_2, 'S&P 1500 SUPER COMPOSITE'] 
   except KeyError:
     try:
       SP1500buydate_2 = SP1500buydate_2 - pd.Timedelta("1 day") 

     except KeyError:
       SP1500buydate_2 = SP1500buydate_2 - pd.Timedelta("2 days") 
   
   Test_set.loc[n, 'Portfolio_2_4'] = Portfolio_2_4
  
   try: 
        SP1500selldate_3 = n_short
        SP1500sellprice_3 = SP1500DailyReturns.loc[SP1500selldate_3, 'S&P 1500 SUPER COMPOSITE - PRICE INDEX']

   except KeyError:
     try:
       SP1500buydate_3 = SP1500buydate_3 - pd.Timedelta("1 day") 

     except KeyError:
       SP1500buydate_3 = SP1500buydate_3 - pd.Timedelta("2 days")  
   
   Portfolio_3_4 = Portfolio_3_4 * SP1500sellprice_3 / SP1500buyprice_3
   SP1500buydate_3_SP1500 = n_short
   Test_set.loc[n, 'Portfolio_3_4'] = Portfolio_3_4

 else:
   Test_set.loc[n, 'Portfolio_1_4_allocation'] = 'Portfolio 1 is already allocated!'
   Test_set.loc[n, 'Portfolio_1_4'] = Portfolio_1_4
     
   try: 
        SP1500selldate_3 = n_short
        SP1500sellprice_3 = SP1500DailyReturns.loc[SP1500selldate_3, 'S&P 1500 SUPER COMPOSITE - PRICE INDEX']

   except KeyError:
     try:
       SP1500buydate_3 = SP1500buydate_3 - pd.Timedelta("1 day") 
       SP1500buydate_3_str = SP1500buydate_3.strftime("%Y-%m-%d")

     except KeyError:
       SP1500buydate_3 = SP1500buydate_3 - pd.Timedelta("2 days") 
       SP1500buydate_3_str = SP1500buydate_3.strftime("%Y-%m-%d")

           
   Portfolio_3_4 = Portfolio_3_4 * SP1500sellprice_3 / SP1500buyprice_3
   SP1500buydate_3_SP1500 = n_short
   Test_set.loc[n, 'Portfolio_3_4'] = Portfolio_3_4

   else:    # Error arises here (!)
     Test_set.loc[n, 'Portfolio_2_4_allocation'] = 'Portfolio 1 and 2 are already allocated!'
     Test_set.loc[n, 'Portfolio_2_4'] = Portfolio_2_4
     if (Portfolio_3_4_Date_sell < pd.Timestamp(Test_set.loc[n, 'feedTimestamp'])):
       Portfolio_3_4 = Portfolio_3_4 * Test_set.loc[n, 'Cumulative stock Returns over the 30 days after transaction  (22 working days)'] 
       Test_set.loc[n, 'Portfolio_3_4'] = Portfolio_3_4
       Portfolio_3_4_Date_sell = Date_Buy + pd.Timedelta("30 days")
     else:
       Test_set.loc[n, 'Portfolio_3_4_allocation'] = 'Portfolio 1, 2 and 3 are already allocated!'
       Test_set.loc[n, 'Portfolio_3_4'] = Portfolio_3_3

If you are trying如果你正在尝试

try:
    #something
except KeyError:
    #something
else:
    #something

then there cannot be anything in between them at the indentation level of the try .那么在try的缩进级别,它们之间不能有任何东西。 Your code is something like你的代码是这样的

try:
    #something
except KeyError:
    #something
#something
else:
    #something

which is why interpreter is reporting a syntax error.这就是解释器报告语法错误的原因。

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

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