Advertisement
Guest User

guessNum.py

a guest
Dec 14th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. import math
  2. import random
  3.  
  4. wrong =   True
  5. guesses = 0
  6. low =     1
  7. high =    10
  8. myNum =   random.randint(low, high)
  9. # Bisect speed on sorted list is O(logN). That will be baseline for a good number of guesses.
  10. goodNum = math.ceil(math.log(high-low))
  11. badNum =  goodNum*2
  12.  
  13. def stats():
  14.     if wrong:
  15.         print ("My number was %i in case you were wondering... " % (myNum))
  16.         print ("You gave up after %i guesses...pathetic" % (guesses))
  17.     else:
  18.         print ("You got it in %i guesses." % (guesses))
  19.         if guesses < goodNum:
  20.             print ("That's really good! (better than a search algorithm!!!)")
  21.         if guesses >= goodNum and guesses < badNum:
  22.             print ("That's alright, I guess.")
  23.         if guesses >= badNum:
  24.             print ("That's kinda pathetic...")
  25.     print ("Thanks for playing :)")
  26.  
  27. while True:
  28.     try:
  29.         guess = input("Guess my number! It's between %i and %i, inclusive (or type 'n' to quit):\n" % (low, high))
  30.         if guess is "n":
  31.             stats()
  32.             break
  33.         guess = int(guess)
  34.     except:
  35.         print("That wasn't a number, you idiot!")
  36.         continue
  37.    
  38.     guesses += 1
  39.  
  40.     # Perform inequality checks to see if guess is correct.
  41.     if guess < myNum: print("Your guess was too low, you pessimistic conservative! Keep Guessing...")
  42.     if guess > myNum: print("Your guess was too high, you pie-in-the-sky liberal! Keep Guessing...")
  43.     if guess == myNum:
  44.         print ("That's right! (you filthy mind-reader)")
  45.         wrong = False
  46.         stats()
  47.        
  48.         # pylint: disable=undefined-variable
  49.         if input("Do you want to play again? (y/n):\n") != "y":
  50.             break
  51.         # If the user wants to play again after winning, reset guesses and myNum.
  52.         guesses = 0
  53.         myNum   = random.randint(low, high)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement