Skip to content

Instantly share code, notes, and snippets.

@umair-tp
Created February 27, 2018 12:37
Show Gist options
  • Select an option

  • Save umair-tp/d5088d3d2a0994a31542a11e2e43de46 to your computer and use it in GitHub Desktop.

Select an option

Save umair-tp/d5088d3d2a0994a31542a11e2e43de46 to your computer and use it in GitHub Desktop.
Exercise 25 : In a previous exercise, we’ve written a program that “knows” a number and asks a user to guess it. This time, we’re going to do exactly the opposite. You, the user, will have in your head a number between 0 and 100. The program will guess a number, and you, the user, will say whether it is too high, too low, or your number. At the …
numbers = range(100)
low = 0
tries = 0
high = len(numbers) - 1
while True:
print str(low) + " : " + str(high)
tries += 1
if low == high:
print "You must have choosed : " + numbers[low]
break
mid = (low + high) / 2 + 1
number = numbers[mid]
if (raw_input("Have you choosed '" + str(number) + "' ? y/n : ") == 'y'):
print "Great! I found it."
break
else:
print "Ah! Ok no worries. I have few more tries left."
print "Enter 1 if your choosed number is less than : " + str(number)
print "Enter 2 if your choosed number is larger than : " + str(number)
choice = raw_input("Enter your options : ")
if choice == '1':
high = mid - 1
elif choice == '2':
low = mid + 1
print "I guessd the right answer in : " + str(tries) + " tries"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment