当条件不满足时,如何引发Exception并返回none值
我是python新手,只需要学习python。我已经编写了一个代码来查找两个字符串中的公共字符,并且得到了所需的输出。如果出现以下情况,我想修改代码,对于以下情况,它应该返回当条件不满足时,如何引发Exception并返回none值,exception,Exception,我是python新手,只需要学习python。我已经编写了一个代码来查找两个字符串中的公共字符,并且得到了所需的输出。如果出现以下情况,我想修改代码,对于以下情况,它应该返回None 1) For two string, if there is no match 2) any of string1 or string2 is nil/empty 3) any of string1 or string2 is hash/array/set/Fixnum [i.e anything other th
None
1) For two string, if there is no match
2) any of string1 or string2 is nil/empty
3) any of string1 or string2 is hash/array/set/Fixnum [i.e anything other than string]
我应该为上述情况提出一个例外。我已经通过论坛和链接,但不能正确地理解它。有人能帮我解决上述情况下如何引发异常吗
1) For two string, if there is no match
2) any of string1 or string2 is nil/empty
3) any of string1 or string2 is hash/array/set/Fixnum [i.e anything other than string]
这是密码
1) For two string, if there is no match
2) any of string1 or string2 is nil/empty
3) any of string1 or string2 is hash/array/set/Fixnum [i.e anything other than string]
class CharactersInString:
def __init__(self, value1, value2):
self.value1 = value1
self.value2 = value2
def find_chars_order_n(self):
new_string = [ ]
new_value1 = list(self.value1)
new_value2 = list(self.value2)
print( "new_value1: ", new_value1)
print( "new_value2: ", new_value2)
for i in new_value1:
if i in new_value2 and i not in new_string:
new_string.append(i)
final_list = list(new_string)
return ''.join(final_list)
if __name__ == "__main__":
obj = CharactersInString("ho", "killmse")
print(obj.find_chars_order_n())
有人能帮我吗有人能帮我吗。。我无法为try-and-except块找出正确的synatx