【发布时间】: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中的return在if语句中。这就是你想要的吗? -
这是错误的:
secondpartsp = secondpartsp.append([self.currency,'1Y'])。append返回None,在返回之前覆盖secondpartsp。 -
@MisterMiyagi 在返回之前需要覆盖它
-
@MatthewStrawbridge 是的,这就是我们的意图
标签: python if-statement return