Skip to content

Instantly share code, notes, and snippets.

@sarmadgulzar
Created August 6, 2019 18:18
Show Gist options
  • Save sarmadgulzar/7a3d4bc3176aa9f66de77f15414e1c4d to your computer and use it in GitHub Desktop.
Save sarmadgulzar/7a3d4bc3176aa9f66de77f15414e1c4d to your computer and use it in GitHub Desktop.
A simple program in which user assumes a number between 0 and 100 and computer tries to guess it. When it guesses the number correctly, we display how many tries it took to reach the correct answer.
import random
lower_bound = 0
upper_bound = 100
random_number = int
guess_count = 1
print('Think of a number between 0-100 in your head, and I will try to guess it.')
print('Enter C for correct, L if it is lower and H if it is higher.')
def generate_random():
try:
return random.randint(lower_bound, upper_bound)
except ValueError:
print("ERROR")
quit()
while True:
random_number = generate_random()
user_response = input('Is it {}? C|L|H: '.format(random_number)).upper()
if user_response == 'C':
print('Yay, I guessed it in {} attempt(s).'.format(guess_count))
break
elif user_response == 'L':
lower_bound = random_number + 1
guess_count += 1
elif user_response == 'H':
upper_bound = random_number - 1
guess_count += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment