Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Guessing Game
- def getGuess(min,max):
- middle = int((max + min)/2)
- answer = input("Is your number [H]igher, [L]ower or the [S]ame as {}: ".format(middle)).upper()
- return middle,answer
- def checkGuess(iMin,iMax,answer,match):
- if answer == "H":
- iMin = int((iMax + iMin)/2)
- match = False
- elif answer == "L":
- iMax = int((iMax + iMin)/2)
- match = False
- else:
- match = True
- return iMin,iMax,match
- max = 101
- min = 0
- print("Think of a number between 1 and 100 inclusive")
- match = False
- valid = True
- guess = 0
- while not match and valid:
- guess = guess + 1
- middle,answer = getGuess(min,max)
- min,max,match = checkGuess(min,max,answer,match)
- if abs(min - max) <= 1:
- valid = False
- if valid:
- if guess == 1:
- guesses = "guess"
- else:
- guesses = "guesses"
- print(f"Your number is {middle}, it took me {str(guess)} {guesses}")
- else:
- print('Impossible. Your number was meant to be an integer between 1 and 100 inclusive')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement