Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- wrong = True
- guesses = 0
- low = 1
- high = 10
- myNum = random.randint(low, high)
- # Bisect speed on sorted list is O(logN). That will be baseline for a good number of guesses.
- goodNum = math.ceil(math.log(high-low))
- badNum = goodNum*2
- def stats():
- if wrong:
- print ("My number was %i in case you were wondering... " % (myNum))
- print ("You gave up after %i guesses...pathetic" % (guesses))
- else:
- print ("You got it in %i guesses." % (guesses))
- if guesses < goodNum:
- print ("That's really good! (better than a search algorithm!!!)")
- if guesses >= goodNum and guesses < badNum:
- print ("That's alright, I guess.")
- if guesses >= badNum:
- print ("That's kinda pathetic...")
- print ("Thanks for playing :)")
- while True:
- try:
- guess = input("Guess my number! It's between %i and %i, inclusive (or type 'n' to quit):\n" % (low, high))
- if guess is "n":
- stats()
- break
- guess = int(guess)
- except:
- print("That wasn't a number, you idiot!")
- continue
- guesses += 1
- # Perform inequality checks to see if guess is correct.
- if guess < myNum: print("Your guess was too low, you pessimistic conservative! Keep Guessing...")
- if guess > myNum: print("Your guess was too high, you pie-in-the-sky liberal! Keep Guessing...")
- if guess == myNum:
- print ("That's right! (you filthy mind-reader)")
- wrong = False
- stats()
- # pylint: disable=undefined-variable
- if input("Do you want to play again? (y/n):\n") != "y":
- break
- # If the user wants to play again after winning, reset guesses and myNum.
- guesses = 0
- myNum = random.randint(low, high)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement