如果Jython FDMEE中的elif(条件)一直给出错误的结果

时间:2018-05-31 09:57:29

标签: jython

如果是jython中的elif(条件),它应该是一个简单的,但似乎FDMEE中的Jython一直在检查条件中的错误结果。

def Timetest(strField, strRecord):  
    import java.util.Date as date  
    import java.text.SimpleDateFormat as Sdf  
    import java.lang.Exception as Ex   
    import java.sql as sql  
    import java.util.concurrent.TimeUnit as TimeUnit  


    PerKey = fdmContext["PERIODKEY"]  
    strDate = strRecord.split(",")[11]  

    #get maturity date  
    strMM = strDate.split("/")[0]  
    strDD = strDate.split("/")[1]  
    strYYYY = strDate.split("/")[2]  
    strDate = ("%s.%s.%s" % (strMM,strDD, strYYYY))  
    #converting Maturity date   
    sdf = Sdf("MM.dd.yyyy")  
    strMRD = sdf.parse(strDate)  
    #calc date diff  
    diff = (strMRD.getTime()- PerKey.getTime())/86400000  
    diff = ("%d" % diff)  
    if diff>="0":  
        if diff <= "30":  
            return  "Mat_Up1m " + diff  
    elif diff <= "90":  
        return "Mat_1to3m " + diff #the result goes here all the time although my diff is 367  
    elif diff <= "360":  
        return  "Mat_3to12m " + diff   
    elif diff <= "1800":  
        return "Mat_1to5y " + diff  #the result supposed to go here  
    else:  
        return  "Mat_Over5y "+ diff  

不确定为什么它继续前往第二个elif而不是第四个elif。

我的计算结果为diff = 367

关于如何确保我的代码读取正确的if ifif条件的任何想法?

1 个答案:

答案 0 :(得分:0)

希望你已经弄明白了,如果没有,请检查你在剧本中犯的错误。

  • 在你的脚本中,你将字符串“367”与字符串值“0”,“90”等进行比较,它不是integer comparison,字符串“367”总是小于字符串“90”所以它的去进入那种精英状态。
  • 现在您必须执行integer comparison instead of string comparison并在主if条件中移动所有elif条件。
  • 在return语句中,您必须将interger to string转换为concatinate diff到字符串。

检查以下代码,包含所有更改。

if diff>=0:  
    if diff <= 30:  
        return  "Mat_Up1m " + str(diff)  
    elif diff <= 90:  
        return "Mat_1to3m " + str(diff)  
    elif diff <= 360:  
        return  "Mat_3to12m " + str(diff)   
    elif diff <= 1800:  
        return "Mat_1to5y " + str(diff)
    else:
        return  "Mat_Over5y "+ str(diff)