Python 如果代码不返回任何值,而应返回指定值
多次检查我的循环。我发现不了一个错误,需要另一双眼睛来帮助。下面的代码返回none,但它应该返回一个特定的值 我已经检查了self.mdlname的值,它是正确的。我认为这与第一个函数curr有关。未正确地将值重新指定给self.currency。我看不出它应该返回none值的其他原因 spartSABR函数后的以下ommitted函数Python 如果代码不返回任何值,而应返回指定值,python,if-statement,return,Python,If Statement,Return,多次检查我的循环。我发现不了一个错误,需要另一双眼睛来帮助。下面的代码返回none,但它应该返回一个特定的值 我已经检查了self.mdlname的值,它是正确的。我认为这与第一个函数curr有关。未正确地将值重新指定给self.currency。我看不出它应该返回none值的其他原因 spartSABR函数后的以下ommitted函数 import string datatype = "Inflation SABR Vol ATM ZC" dataconvention = "UK-RPI-Z
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())
secondpartsp=secondpartsp.append([self.currency,'1Y']) 这会将[self.currency,'1Y']追加到secondpartsp。但是append方法不会返回任何内容(它不会返回任何内容),它不会像您所想的那样返回列表。因此,接下来发生的事情是将结果(无)分配给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())
基于恒河湿婆克里希纳提供的逻辑。我解决了这个问题。我不需要同时赋值和追加。解决方案如下:
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
curr
中的return
在if
语句中。这就是你想要的吗?这是错误的:secondpartsp=secondpartsp.append([self.currency,'1Y'])
append
返回None
,在返回前覆盖secondpartsp
。它可能重复。@MisterMiyagi在返回前需要覆盖它returned@MatthewStrawbridge是的,这就是我们的意图