我的代码在某些测试用例上返回一个异常,在其他测试用例上返回TRUE。如何在应用条件时替换字符串中的空格
CASE 1:
样本输入:
<br>source = 'Delhi'<br>
destination = 'Agra'<br>
Type = 'one-way'
预期输出:
if exist -> return 'already exist' else: insert into dB.
我的输出:
正常工作
案例2:
样品输入:
source = 'Delhi'<br>
destination = 'A gra'<br>
Type = 'one-way'<br>
预期输出:
if exist -> return 'already exist' else: insert into dB.
我的输出:
Error: local variable 'source' referenced before assignment
如果我指定变量None
它会展现成功者的成功等。
res = db.test("select source,destination,type from cities where source = '"+str(self.source)+"' and destination='"+str(self.destination)+"' and type = '"+str(self.type)+"'")
for row in res:
source = row['source']
destination = row['destination']
types = row['type']
src = self.source
dst = self.destination
typ = self.type
if str(src).replace(' ','').lower() == str(source).replace(' ','').lower() and str(dst).replace(' ','').lower() == str(destination).replace(' ','').lower() and str(typ).replace(' ','').lower() == str(types).replace(' ','').lower():
return "already exist"
哪个例外?当你不给我们任何信息时,你如何期待我们帮助你解决错误 –
向我们展示一个完整的追溯。你确定你的缩进是正确的吗? for循环只应该包含三行? –
你称为**样本输入**是'res'的值?你能否提供它吗?你的代码是一个类的一部分。发布之前,您是否在课堂之外测试过它?你提供了输出和一个**类型的输入(如果'res'是输入,那么它是一个字典,一个列表,一个列表中的字典?一个字符串?......),但是你没有提供错误和一个体面的代码工作... –