39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
print("Please think of a number between 0 and 100!")
|
|
guess = input()
|
|
|
|
computer_guess = 50
|
|
user_input = ""
|
|
correct_input = False
|
|
|
|
def checkUserInput(user_input):
|
|
if user_input == 'l' or user_input == 'h' or user_input == 'c':
|
|
return True
|
|
else:
|
|
print("Sorry, I did not understand your input.")
|
|
return False
|
|
|
|
def checkValue(user_input, guess):
|
|
if user_input == 'h':
|
|
new_guess = guess//2
|
|
return new_guess
|
|
elif user_input == 'l':
|
|
new_guess = guess + guess//2
|
|
return new_guess
|
|
elif user_input == 'c':
|
|
return guess
|
|
else:
|
|
raise ValueError
|
|
|
|
while guess != computer_guess:
|
|
print("Is your secret number", computer_guess, "?")
|
|
print("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.")
|
|
user_input = input()
|
|
if correct_input == False:
|
|
correct_input = checkUserInput(user_input)
|
|
computer_guess = checkValue(user_input, computer_guess)
|
|
else:
|
|
computer_guess = checkValue(user_input, computer_guess)
|
|
|
|
|
|
print("Game over. Your secret number was:", computer_guess)
|