0

my code seems to work up to the line before WHILE loop. The idea is to have the computer try to guess the number inputted by Player 1('P1') by picking a middle number from the lowest and highest number of previous guesses. I cant figure out why does it keep looping!

import random
P1 = raw_input("The number should be between 0 to 100: \n") 
P1 = int(P1)
previousguess = []
count2 = 0
for c in range(2):
 cg2 = random.randint(0,100)
 print "Computer has guessed: %d" %cg2
 if P1 != cg2:
 previousguess.append(cg2) #adding the guess into "Previous Guess" list
 print previousguess
mn = min(previousguess)
mx = max(previousguess)
while True:
 if P1 != cg2:
 cg2 = (mn+mx)/2 #guess based on middle of highest and lowest
 print "Computer has guessed: %d" %cg2
 previousguess.append(cg2)
 count2+=1
else:
 print "The Computer has guessed it in %d times" %count2
Ryan Haining
37.2k17 gold badges125 silver badges183 bronze badges
asked Oct 2, 2015 at 0:03
3
  • 3
    You never terminate the loop. When the computer gets the guess correct, the if condition fails, and the program does nothing forever. Commented Oct 2, 2015 at 0:06
  • hi.. thank you for your comments..I have tried each suggestion given but am still unable to stop the while loop.. It only calculated the middle once and continued to loop that result infinitely. Commented Oct 2, 2015 at 4:05
  • As I said in my answer, you have to update mn & mx properly. Your original code doesn't update them at all, and the update it does use is invalid for the binary search algorithm. If you need more help, you have to follow the guidelines -- most notably, post your new code. Commented Oct 2, 2015 at 17:07

3 Answers 3

3

Because you're using while True and True is always equal to True, so this loop will never stop. You can do something like this:

while True:
 if P1 != cg2:
 cg2 = (mn+mx)/2 #guess based on middle of highest and lowest
 print "Computer has guessed: %d" %cg2
 previousguess.append(cg2)
 count2+=1
 else:
 print "The Computer has guessed it in %d times" %count2 
 break # use break to break the loop

Or just like this:

while P1 != cg2:
 cg2 = (mn+mx)/2 #guess based on middle of highest and lowest
 print "Computer has guessed: %d" %cg2
 previousguess.append(cg2)
 count2+=1

if P1 != cg2 is equal to False, this loop will terminated.

answered Oct 2, 2015 at 0:15
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

while P1 != cg2:
 cg2 = (mn+mx)/2 #guess based on middle of highest and lowest
 print "Computer has guessed: %d" %cg2
 previousguess.append(cg2)
 count2+=1

You will also need to properly update mn & mx within the loop. These are not the only problems, but they should get you to the next stage of debugging. Please consider adding print statements to track the progress of your program, both where it goes and what data it computes.

answered Oct 2, 2015 at 0:06

Comments

1

You need to insert a break statement after the computer has guessed the answer.

while True: # is always true

Because it is always true, the loop will never end until you force it do so using break.

Edit: you could also create a conditional as shown in the other answer.

answered Oct 2, 2015 at 0:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.