【问题标题】:if code returning none value while it should return a specified value如果代码没有返回值而它应该返回一个指定的值
【发布时间】:2019-04-03 14:14:25
【问题描述】:

多次检查我的循环。我无法发现错误需要另一双眼睛来帮助。下面的代码没有返回任何值,但它应该返回一个特定的值。

我检查了 self.mdlname 的值,它是正确的。我认为与第一个函数 curr 有关。它没有正确地将值重新分配给 self.currency。我看不出它应该返回 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())

【问题讨论】:

  • curr 中的 returnif 语句中。这就是你想要的吗?
  • 这是错误的:secondpartsp = secondpartsp.append([self.currency,'1Y'])append 返回None,在返回之前覆盖secondpartsp
  • @MisterMiyagi 在返回之前需要覆盖它
  • @MatthewStrawbridge 是的,这就是我们的意图

标签: python if-statement return


【解决方案1】:

secondpartsp = secondpartsp.append([self.currency,'1Y']) 这会将 [self.currency,'1Y'] 附加到 secondpartsp 上。但是 append 方法不返回任何内容(它返回 None),它不会像您想的那样返回列表。所以那么会发生 结果 (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())

【讨论】:

    【解决方案2】:

    基于 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2019-04-27
      • 2019-02-23
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 2011-03-28
      相关资源
      最近更新 更多