Advertisement
JAS_Software

Guessing Game

May 29th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. ## Guessing Game
  2.  
  3. def getGuess(min,max):
  4.     middle = int((max + min)/2)
  5.     answer = input("Is your number [H]igher, [L]ower or the [S]ame as {}: ".format(middle)).upper()
  6.     return middle,answer
  7.  
  8. def checkGuess(iMin,iMax,answer,match):
  9.     if answer == "H":
  10.         iMin = int((iMax + iMin)/2)
  11.         match = False
  12.     elif answer == "L":
  13.         iMax = int((iMax + iMin)/2)
  14.         match = False
  15.     else:
  16.         match = True
  17.     return iMin,iMax,match
  18.    
  19. max = 101
  20. min = 0
  21.  
  22. print("Think of a number between 1 and 100 inclusive")
  23. match = False  
  24. valid = True
  25. guess = 0
  26. while not match and valid:
  27.     guess = guess + 1
  28.     middle,answer = getGuess(min,max)
  29.     min,max,match = checkGuess(min,max,answer,match)
  30.     if abs(min - max) <= 1:
  31.         valid = False
  32.  
  33. if valid:
  34.     if guess == 1:
  35.         guesses = "guess"
  36.     else:
  37.         guesses = "guesses"
  38.     print(f"Your number is {middle}, it took me {str(guess)} {guesses}")
  39. else:
  40.     print('Impossible. Your number was meant to be an integer between 1 and 100 inclusive')
  41.    
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement