Python Assignment Help

Python Program on Guessing Game

import numpy as np
print(”)
print(“Enter two numbers, low then high.”)
l = int(input(“low = “))
h = int(input(“high = “))


def binarySearch (arr, l, r, num):
if r >= l:
mid = l + (r – l) // 2

print(“Is your number Less than, Greater than, or Equal to {0}?”.format(arr[mid]))
while True:
x = str(input(“Type ‘L’, ‘G’ or ‘E’:”))
if x in [‘L’, ‘G’, ‘E’, ‘l’, ‘g’, ‘e’]:
break

print(”)
if x == ‘E’ or x == ‘e’:
num = num + 1
if num == 1:
print(“I found your number in 1 guess.”)
else :
print(“Your number is {0}. I found it in {1} guesses.”.format(arr[mid], num))
elif x == ‘L’ or x == ‘l’:
num = num + 1
return binarySearch(arr, l, mid-1, num)
else:
num = num + 1
return binarySearch(arr, mid + 1, r, num)
else:
print(“Your answers have not been consistent.”)

while l>h:
print(”)
print(“Please enter the smaller followed by the larger number.”)
l = int(input(“low = “))
h = int(input(“high = “))

print(”)
print(“Think of a number in the range {0} to {1}.”.format(l, h))

print(”)
if l == h:
print(“Your number is {0}. I found it in 0 guesses.”.format(l))

else :
arr = np.arange(l+1, h)
binarySearch(arr, 0, len(arr)-1, 0)