Your function takes parameters A
and B
yet you ignore them and assign values to them. Why? Also, you define a parameter self
, which is normally done when the function is a method belonging to a class definition. It serves no purpose here.
As been pointed out, you need to move return ValueError("there is no same value")
out of the loop and execute it only after you have tested all values in A
against all values in B
:
def samevalue(A, B):
for i in range(len(A)):
for j in range(len(B)):
if A[i] == B[j]:
return A[i]
return ValueError("there is no same value")
print(samevalue([4,7,1,9], [5,8,7,2,4]))
Prints:
4
Or (more efficient):
def samevalue(A, B):
for a in A:
if a in B:
return a
return ValueError("there is no same value")
Or (most efficient for large lists):
def samevalue(A, B):
seta = set(A)
setb = set(B)
setc = seta & setb
if not setc:
return ValueError("there is no same value")
return setc.pop()