Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Number Guesser
minimum, maximum, guess_count = 0, 100, 0
print("Please think of a number between 0 and 100!")
while True:
number = int((minimum + maximum) / 2)
user_input = input("Is your secret number " + str(number) + "?\nEnter 'h' to indicate the guess is too high.\n\
Enter 'l' to indicate the guess is too low.\nEnter 'c' to indicate I guessed correctly.\nEnter 'm' if you made a mistake.")
guess_count += 1
if user_input == "c":
break
elif user_input == "h":
maximum = number
elif user_input == "l":
minimum = number
elif user_input == "m":
print("Alrighty then, let's start over.")
minimum, maximum, guess_count = 0, 100, 0
else:
print("Sorry, I did not understand your input.")
guess_count -= 1
correct_guess = "Gotcha! I guessed your secret number " + str(number) + " in " + str(guess_count)
if guess_count == 1:
print(correct_guess + " try. Oh, come on!")
else:
print(correct_guess + " tries.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment