0
Why this code is showing "no output"
3 odpowiedzi
+ 3
for the first time when for loop iterates , i.e. at i=0, the if condition is not matched..
So the control goes to else block and there it finds return False statement, which stops the function and comes out of it..
So either you move else block indentation to match the for loop.. (as Hamid Reza Mousavi stated) or just match the retrun statement with the for loop.
def linear(theval,target):
n=len(theval)
for i in range(n):
if theval[i] == target:
print("success")
return False
And in the second program that is a binary search, for binary search your list should be sorted first.. so first sort the list then apply binary search
def binary(thevals,tarrget):
low=0
high=len(thevals)-1
thevals.sort()
while low<=high :
mid=(high+low)//2
if thevals[mid]==tarrget:
print("trv")
break
elif tarrget < thevals[mid]:
high=mid-1
else:
low=mid+1
return False
0
The function return false in first loop and finish.below code work .else bond with for not with if
def linear(theval,target):
n=len(theval)
for i in range(n):
if theval[i]==target:
print("success")
else:
return False
0
Thank u sami khan