如果代码不返回任何值,而应返回指定值

时间:2019-04-03 14:14:26

标签: python if-statement return

检查了我的循环几次。我无法发现一个错误,需要另一双眼睛来帮助。下面的代码不返回任何内容,而应返回一个特定值。

我已经检查了self.mdlname的值,它是正确的。我认为与第一个函数curr有关。它没有正确地将值重新分配给self.currency。我认为没有其他原因为什么它应该返回无值。

在spartSABR函数之后的遗漏函数

import string

datatype = "Inflation SABR Vol ATM ZC"
dataconvention = "UK-RPI-ZERO-COUPON-SWAP-RATE"
mdlname = 'INFLATION_SABR'
t1 = 'should-send-back-none'


class srtqualifier:
    def __init__(self,mdlname,datatype, dataconvention,t1):
        self.mdlname = mdlname
        self.datatype = datatype
        self.dataconvention = dataconvention
        self.t1 = t1 ##TO BE REMOVED, ONLY USE FOR TESTING##
        self.tempholder =  self.dataconvention.split("-")
        self.currency = self.tempholder[0]

    def curr(self):
        if self.mdlname == 'INFLATION_SABR':
            inflationcurrency = {'UK':'GBP','FR':'EUR','EU':'EUR','US':'USD'}
            self.currency = inflationcurrency.get(self.currency)
            return self.currency

    def spartSABR(self):
        if self.dataconvention == 'JPY-SWAPTION-CLEAREDPHYSICAL-SEMI-ACT365F':
            return 'LIBOR_JPY_6M'
        elif self.dataconvention == 'USD-SIFMA Municipal Swap Index-1W-SWAPTION-PHYSICAL-SEMI-BOND':
            secondpartsp = self.tempholder[1].split(" ")
            secondpartsp.append(self.tempholder[2])
            separator = "_"
            secondpart = separator.join(secondpartsp[1:len(secondpartsp)])
            secondpart = secondpart.upper()
            return secondpart
        elif self.mdlname == 'INFLATION_SABR':
            secondpartsp = self.tempholder[0:2]
            secondpartsp = secondpartsp.append([self.currency,'1Y'])
            return secondpartsp
        else:
            secondpartsp = self.tempholder[1].split(" ")
            secondpartsp.append(self.tempholder[2])
            secondpartsp.insert(1,self.currency)
            separator = "_"
            secondpart = separator.join(secondpartsp)
            secondpart = secondpart.upper()
            return secondpart

test1 = srtqualifier(mdlname,datatype,dataconvention,t1)
print (test1.curr())
print (test1.spartSABR())

2 个答案:

答案 0 :(得分:0)

secondpartsp = secondpartsp.append([self.currency,'1Y']) 这会将[self.currency,'1Y']附加到secondpartsp上。但是append方法不返回任何内容(它返回None),它不会像您想的那样返回列表。那么会发生什么事结果(无)被分配给secondpartsp,现在secondpartsp为空。

import string

datatype = "Inflation SABR Vol ATM ZC"
dataconvention = "UK-RPI-ZERO-COUPON-SWAP-RATE"
mdlname = 'INFLATION_SABR'
t1 = 'should-send-back-none'


class srtqualifier:
    def __init__(self,mdlname,datatype, dataconvention,t1):
        self.mdlname = mdlname
        self.datatype = datatype
        self.dataconvention = dataconvention
        self.t1 = t1 ##TO BE REMOVED, ONLY USE FOR TESTING##
        self.tempholder =  self.dataconvention.split("-")
        self.currency = self.tempholder[0]

    def curr(self):
        if self.mdlname == 'INFLATION_SABR':
            inflationcurrency = {'UK':'GBP','FR':'EUR','EU':'EUR','US':'USD'}
            self.currency = inflationcurrency.get(self.currency)
            return self.currency

    def spartSABR(self):
        if self.dataconvention == 'JPY-SWAPTION-CLEAREDPHYSICAL-SEMI-ACT365F':
            return 'LIBOR_JPY_6M'
        elif self.dataconvention == 'USD-SIFMA Municipal Swap Index-1W-SWAPTION-PHYSICAL-SEMI-BOND':
            secondpartsp = self.tempholder[1].split(" ")
            secondpartsp.append(self.tempholder[2])
            separator = "_"
            secondpart = separator.join(secondpartsp[1:len(secondpartsp)])
            secondpart = secondpart.upper()
            return secondpart
        elif self.mdlname == 'INFLATION_SABR':
            secondpartsp = self.tempholder[0:2]
            secondpartsp.append([self.currency,'1Y'])
            return secondpartsp
        else:
            secondpartsp = self.tempholder[1].split(" ")
            secondpartsp.append(self.tempholder[2])
            secondpartsp.insert(1,self.currency)
            separator = "_"
            secondpart = separator.join(secondpartsp)
            secondpart = secondpart.upper()
            return secondpart

test1 = srtqualifier(mdlname,datatype,dataconvention,t1)
print (test1.curr())
print (test1.spartSABR())

答案 1 :(得分:-1)

基于Ganga Siva Krishna提供的逻辑。我解决了这个问题。我不需要分配和附加在一起。解决方案如下:

def spartSABR(self):
        if self.dataconvention == 'JPY-SWAPTION-CLEAREDPHYSICAL-SEMI-ACT365F':
            return 'LIBOR_JPY_6M'
        elif self.dataconvention == 'USD-SIFMA Municipal Swap Index-1W-SWAPTION-PHYSICAL-SEMI-BOND':
            secondpartsp = self.tempholder[1].split(" ")
            secondpartsp.append(self.tempholder[2])
            separator = "_"
            secondpart = separator.join(secondpartsp[1:len(secondpartsp)])
            secondpart = secondpart.upper()
            return secondpart
        elif self.mdlname == 'INFLATION_SABR':
            secondpartsp = self.tempholder[0:2]
            print(self.tempholder[0:2])
            secondpartsp.append(self.currency)## I was doing secondpartsp= secondpartsp.append(self.currency)
            secondpartsp.append('1Y')
            separator = "_"
            secondpartsp = separator.join(secondpartsp)
            return secondpartsp
        else:
            secondpartsp = self.tempholder[1].split(" ")
            secondpartsp.append(self.tempholder[2])
            secondpartsp.insert(1,self.currency)
            separator = "_"
            secondpart = separator.join(secondpartsp)
            secondpart = secondpart.upper()
            return secondpart