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

2024-04-24 05:22:47 发布

您现在位置:Python中文网/ 问答频道 /正文

检查了我的循环好几次。我发现不了一个错误,我需要另一双眼睛来帮忙。下面的代码返回none,而它应该返回一个特定的值。你知道吗

我已经检查了self.mdl名称这是正确的。我想这和第一个函数curr有关。将值重新分配给本币. 我看不出它应该返回none值的其他原因。你知道吗

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())

Tags: 函数selfnonereturncurrencyt1datatypeseparator
2条回答

基于恒河湿婆克里希纳提供的逻辑。我解决了问题。我不需要分配和附加在一起。解决方案如下:

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

第二部分sp=第二部分附加([本币,'1Y']) 这个附加[本币,'1Y']到第二部分但是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())

相关问题 更多 >