def yesNo( prompt ):
"""Ask the user the prompt, until the user replies with with a 'y' or a 'n' (upper or lowercase)"""
response = ""
while response != 'y' and response != 'Y' and response != 'n' and response != 'N':
response = input( prompt )
if response == 'y' or response == 'Y':
answer = True
else:
answer = False
return answer
print("Welcome to the guessing game, where the computer tries to guess what number you're thinking of (0-100)")
playTheGame = True
while playTheGame:
rightAnswer = False
low = 0
high = 100
while rightAnswer == False:
mid = low + (high - low)//2
if mid < low or mid > high:
cheated = yesNo( "Wait a second, did you cheat? (Y/N) ")
if cheated:
print("Let me suggest some reading for you: https://www.acm.org/about-acm/code-of-ethics :)")
else:
print("Oh snap! Something went wrong. Let's play again :)")
break
rightAnswer = yesNo( "Are you thinking of " + str(mid) + "? (Y/N) ")
if rightAnswer == False:
guessHigher = yesNo( "Are you thinking of a number higher than " + str(mid) + "? (Y/N) ")
if guessHigher:
low = mid + 1
else:
high = mid - 1
playTheGame = yesNo( "Great game! Do you want to play again? (Y/N) ")
print("Bye!")