I’m trying to write a python program in which the program guesses the users number. I fel like an idiot, the part i am getting caught up on is the math, i feel like its one of those things where I’ve been working on it for a few hours and someone else would be able to bring a fresh perspective and solve it easily. I figured to start off I would take the user component out of it almost completely. Here is what I have so far, its getting stuck on the logic part and eventually just keeps saying its either too low or too high, for infinity:
MIN=0
MAX=100
firstguess = MAX - MIN
firstguess = firstguess/2
while 1==1:
number = int(raw_input("Enter your number 0-100:"))
print firstguess
oldguess = firstguess
if firstguess > number:
print "First guess is too high."
raw_input()
guess = int(25)
print guess
while guess != number:
if guess > number:
print "My guess was too high."
raw_input()
nextguess = oldguess - guess
nextguess = nextguess/2
nextguess = guess - nextguess
oldguess = guess
guess = nextguess
elif guess == number:
print "I win!"
exit
elif guess < number:
print "My guess was too low."
raw_input()
nextguess = oldguess - guess
nextguess = nextguess/2
nextguess = nextguess + guess
oldguess = guess
guess = nextguess
elif firstguess == number:
print "I win!"
elif firstguess < number:
print "My first guess was too low."
raw_input()
guess = 75
print guess
print guess
while guess != number:
if guess > number:
print "My guess was too high."
raw_input()
nextguess = oldguess - guess
nextguess = nextguess/2
nextguess = guess - nextguess
oldguess = guess
guess = nextguess
elif guess == number:
print "I win!"
exit
elif guess < number:
print "My guess was too low."
raw_input()
nextguess = oldguess - guess
nextguess = nextguess/2
nextguess = nextguess + guess
oldguess = guess
guess = nextguess
I might be doing your homework, but you seemed to make an honest attempt at the problem:
The logic is simple:
number.