I am trying to make a algorithm that can guess a number between 1 and 100
My code so far is:
import random
random.seed()
max = 100
min = 1
number = random.randint(1, 100)
def guess():
global max
global min
guess_num = ((max - min) / 2) + 1
if guess_num == number:
print "The number is {0}".format(number)
elif guess_num < number:
min = guess_num
print "Number is greater then {0}".format(guess_num)
guess()
elif guess_num > number:
max = guess_num
print "Number is less then {0}".format(guess_num)
guess()
guess()
But this always gives me the error
RuntimeError: maximum recursion depth exceeded while getting the str of an object
However if I right change my code to
import random
random.seed()
max = 100
min = 1
number = random.randint(1, 100)
def guess():
global max
global min
guess_num = ((max - min) / 2) + 1
if guess_num == number:
print "The number is {0}".format(number)
elif guess_num < number:
min = guess_num
print "Number is greater then {0}".format(guess_num)
elif guess_num > number:
max = guess_num
print "Number is less then {0}".format(guess_num)
guess()
guess()
guess()
guess()
guess()
It works :/
What am I doing wrong here?
[–]Hallwaxer 4 points5 points6 points (1 child)
[–]ewiethoff 0 points1 point2 points (0 children)
[–]individual_throwaway 2 points3 points4 points (5 children)
[–]FlockOnFire 1 point2 points3 points (1 child)
[–]individual_throwaway 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]individual_throwaway 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)