Sep-01-2017, 06:54 AM
Hello friends,given program show given error. Please give me solution.
i want '[2,4,7,8,19]' as list of element but it is string.
ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]'[/python]
i want '[2,4,7,8,19]' as list of element but it is string.
ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]'[/python]
def binarySearch (arr, first, last, element): if last < 1: return -1 else: mid = first+ (last-1)//2 if arr[mid] == element: return mid elif arr[mid] >element: return binarySearch(arr, first, mid-1, element) else: return binarySearch(arr, mid+1, last, element) arr=int(input("Enter list of element:")) print(len(arr)) #Enter list of element:[2,5,7,9,10] element=int(input("Enter list of element:")) print(element) #arr = [ 2, 3, 4, 10, 40 ] #print(len(arr)) #element = 10 result = binarySearch(arr, 0, len(arr)-1, element) if result != -1: print("Element is present at index %d" % result) else: print("Element is not present in array") Output: Enter list of element:[2,4,7,8,19] Traceback (most recent call last): File "C:\Python 3.6\program\binary1.py", line 13, in <module> arr=int(input("Enter list of element:")) ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]'