Created
March 13, 2010 18:24
-
-
Save Ttech/331472 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import math, random | |
guess_range = [10,100] | |
number = 0 | |
while (number < 1) and (number != ""): | |
number = raw_input("\nPlease enter a number for the computer to guess: ") | |
# make it an integer | |
number = int(number) | |
logic = random.randrange(1,500) + 1 | |
tries = 1 | |
while logic is not number: | |
if logic > number: | |
guess_range[1] = logic | |
elif logic < number: | |
guess_range[0] = logic | |
else: | |
print "I guessed it! The number was, ", logic | |
break | |
if guess_range[0] >= guess_range[1]: | |
guess_range[1] = guess_range[0] + guess_range[1] | |
logic = random.randrange(guess_range[0],guess_range[1]) + 1 | |
print tries, " [", guess_range[0], ",", guess_range[1],"][ ", logic, ", ", number, "]\n" | |
tries += 1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import math, random | |
guess_range = [0,100] | |
number = 0 | |
# Wait till the user gets input, then go! | |
while (number < 1) and (number != ""): | |
number = raw_input("\nPlease enter a number for the computer to guess: ") | |
# make it an integer | |
number = int(number) | |
# The computer's guess variable / seed | |
seed = random.randrange(1,500) + 1 | |
tries = 1 | |
# Create a loop that checks to see if the number the computer 'seeds' is the same as the number the user input. | |
while seed is not number: | |
# Check to see if the number is great, then add it to high range | |
if seed > number: | |
guess_range[1] = seed | |
# check to see if the number is lower then add it to the low range. | |
elif seed < number: | |
guess_range[0] = seed | |
# We assume that the only other possible option is that the answer whas guessed, we break loop | |
else: | |
print "I guessed it! The number was, ", seed | |
break | |
# This part is important, to prevent large numbers from having the low range be higher then the high range | |
# we need to add both of them together to make a larger numer for the high range for the computer to work with | |
# this allows the computer to continue to guess untill its rached the high number. | |
if guess_range[0] >= guess_range[1]: | |
guess_range[1] = guess_range[0] + guess_range[1] | |
# Now we need to make a random number for the computer's guess or 'seed' This guess is from the high and low ranges as defined above | |
seed = random.randrange(guess_range[0],guess_range[1]) + 1 | |
# print and increment | |
print tries, " [", guess_range[0], ",", guess_range[1],"][ ", seed, ", ", number, "]\n" | |
tries += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
T [ low, high] [seed, number] | |
1 [ 0 , 134 ][ 27 , 42 ] | |
2 [ 27 , 134 ][ 40 , 42 ] | |
3 [ 40 , 134 ][ 131 , 42 ] | |
4 [ 40 , 131 ][ 66 , 42 ] | |
5 [ 40 , 66 ][ 43 , 42 ] | |
6 [ 40 , 43 ][ 42 , 42 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment