0

Why this code is showing "no output"

https://code.sololearn.com/cl1xoL2x4E2Q/?ref=app

21st Nov 2019, 3:21 PM
Thakur LionšŸ˜ŽšŸ˜Ž
Thakur LionšŸ˜ŽšŸ˜Ž - avatar
3 Answers
+ 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
21st Nov 2019, 3:56 PM
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ 
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ  - avatar
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
21st Nov 2019, 3:47 PM
Hamid Reza Mousavi
Hamid Reza Mousavi - avatar
0
Thank u sami khan
22nd Nov 2019, 3:36 PM
Thakur LionšŸ˜ŽšŸ˜Ž
Thakur LionšŸ˜ŽšŸ˜Ž - avatar