user2066771
user2066771

Reputation: 153

Taking a Python course and learning about bisection search, confused on why my semantics are incorrect

Our assignment is to write a code that will guess a secret number from 0 to 100. This is my piece of code:

low = 0
mid = 50
high = 100
secretnum = "Is your secret number " + str(mid) + "?"
print"Please think of a number between 0 and 100!"
print secretnum
herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
while herp != 'c':
    if herp == 'h':
        high = mid
        mid = int((mid + low)/2)
    elif herp == 'l':
        low = mid
        mid = int((mid + high)/2)
    else:
        print"Sorry, I did not understand your input."
    print secretnum
    herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
if herp == 'c':
    print "Game over. Your secret number was: " + str(mid)

This is the output:

Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. c
Game over. Your secret number was: 37

Upvotes: 1

Views: 3662

Answers (8)

Bikramjeet Singh
Bikramjeet Singh

Reputation: 688

You don't have to include int((mid + low)/2)under every if-elif block. Here's the approved code for the same exercise:

print("Please think of a number between 0 and 100!")
low = 0
high = 100
guess = (low + high) // 2

while True:
    print("Is your secret number " + str(guess) + "?")
    suggest = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
    if suggest == 'l':
        low = guess
    elif suggest == 'h':
        high = guess
    elif suggest == 'c':
        print("Game over. Your secret number was: " + str(guess))
        break
    else:
        print("Sorry, I did not understand your input.")
    guess = (low + high) // 2

Upvotes: 0

rakesh gupta
rakesh gupta

Reputation: 21

Try below code...

max_num=100
min_num=0
guess = int(abs(max_num/2))
print("Please think of a number between 0 and 100!")
for i in range(min_num,max_num):
  text = "Is your secret number "+str(guess)+"?\nEnter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. "
  xx = raw_input(text)
  if(xx == 'h'):
    max_num = guess
    guess = int((guess + min_num)/2)
  elif(xx == 'l'):
    min_num = guess
    guess = int((guess + max_num)/2)
  elif(xx == 'c'):
    print("Game over. Your secret number was:"+str(guess))
  else:  
    print("Sorry, I did not understand your input.")
    xx = str(input(text))

Upvotes: 0

FabioRosado
FabioRosado

Reputation: 503

When I was building my code I had the same problem at first. After looking over your code I've noticed few errors and I'll highlight them here:

  1. You did not used raw_input to ask the user for the number
  2. You shouldn't use the 'c' parameter for your while
  3. Your while should come after the raw_input question

    num = raw_input("Please think of a number between 0 and 100!")

    while mid != num:
        print ( "Is your secret number " + str(mid) + "?")
        herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
    

See this here, you use the while so it runs until the mid is different than the number that the user chose, it's very important to have that print therem otherwise you wont be able to see the new guess of the program after you chose if the first guess was too low or too high.

  1. There is no need to repeat the piece "mid = int(low + high)/2" over and over, just use it after your else and each time the while loop runs it will change the value of mid.
  2. you need to start the if/elif/else code with the letter 'c'
  3. You can delete the "print secretnum" line and the one after that

    if herp == 'c':

            break
        elif herp == 'l':
            low = mid
    
        elif herp == 'h':
            high = mid
    
        else:
            print"Sorry, I did not understand your input."
        mid = int(low + high)/2
    print "Game over. Your secret number was: " + str(mid)
    

    Notice that I used the "break" to leave the while loop and so the message Game over. Your secret number was: " + str(mid) is printed.

The final result should look like this:

low = 0
high = 100
mid = (high + low)/2

num = raw_input("Please think of a number between 0 and 100!")

while mid != num:
    print ( "Is your secret number " + str(mid) + "?")
    herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")

    if herp == 'c':
        break
    elif herp == 'l':
        low = mid
        #mid = int(mid + high)/2
    elif herp == 'h':
        high = mid
        #mid = int(mid + low)/2
    else:
        print"Sorry, I did not understand your input."
    mid = int(low + high)/2
print "Game over. Your secret number was: " + str(mid)

Upvotes: 0

Stephen
Stephen

Reputation: 2424

for the error change it to:

print "Game over. Your secret number was:",mid

For the outputting 50 over and over, change print secretnum in the while to:

print "Is your secret number " + str(mid) + "?"

when you set secretnum="Is your secret number " + str(mid) + "?" at the beginning, it creates a string, a string completely separate from mid. So if you change mid, the change wont be seen in the string.

Python strings are immutable meaning that once they are made, they are done. You can't change the content of a string, without completely remaking it. What str(mid) does is create a string representation of mid. In this case the string "50" is created and put into the string, never to be modified. So when you are displaying a string you need to make sure it is displaying the most recent value by calling str(mid) again.

Upvotes: 2

Adam Kurkiewicz
Adam Kurkiewicz

Reputation: 1632

in the last line you simply put:

print "Game over. Your secret number was: " + str(mid)

This is because python wants to make sure you really know what you're doing -- that is adding two strings together, not an int and a string -- which is forbidden. The str() function simply changes anything you give it to a string. With regards to your semantics problems, I think this version of the code has got expected behaviour:

low = 0  
mid = 50  
high = 100  
print "Please think of a number between 0 and 100!"  
herp = 50  
while herp != 'c':  
    print "Is your secret number " + str(mid) + "?"
    herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is to low. Enter 'c' to indicate the guess is correct")   
    if herp == 'h':  
        high = mid  
        mid = int((mid + low)/2)  
    elif herp == 'l':  
        low = mid  
        mid = int((mid + high)/2)  
    else:  
        print"Sorry, I did not understand your input."  
if herp == 'c':  
    print "Game over. Your secret number was: " + str(mid) 

Upvotes: 1

Talvalin
Talvalin

Reputation: 7889

As Raufio pointed out, Python strings are immutable. To get around the problem with 50 being repeated over and over, you need to call str(mid) again when you print out the question. For example:

low = 0
mid = 50
high = 100
secretnum = "Is your secret number: " 
print"Please think of a number between 0 and 100!"
print secretnum + str(mid) + "?"
herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
while herp != 'c':
    if herp == 'h':
        high = mid
        mid = int((mid + low)/2)
    elif herp == 'l':
        low = mid
        mid = int((mid + high)/2)
    else:
        print"Sorry, I did not understand your input."
    print secretnum + str(mid) + "?"
    herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
if herp == 'c':
    print "Game over. Your secret number was:", mid

Upvotes: 2

Michael0x2a
Michael0x2a

Reputation: 64058

At the very last line, you typed print "Game over. Your secret number was: " + mid. However, mid is a number, and adding it to a string doesn't make any sense. You need to convert it to a string first.

There are several ways to do this. You could change it to:

  • print "Game over. Your secret number was: " + str(mid)
    ...to turn mid into a string
  • print "Game over. Your secret number was: ", mid
    ...so that Python will treat the string and mid as two variables, and automatically add a space between them

.

low = 0
mid = 50
high = 100
secretnum = "Is your secret number " + str(mid) + "?"
print"Please think of a number between 0 and 100!"
print secretnum
herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
while herp != 'c':
    if herp == 'h':
        high = mid
        mid = int((mid + low)/2)
    elif herp == 'l':
        low = mid
        mid = int((mid + high)/2)
    else:
        print"Sorry, I did not understand your input."
    print secretnum
    herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
if herp == 'c':
    print "Game over. Your secret number was: " + str(mid)

Upvotes: 0

Josh Austin
Josh Austin

Reputation: 776

Change line 20

print "Game over. Your secret number was: " + mid

to

print "Game over. Your secret number was: ", mid

or

print "Game over. Your secret number was: " + str(mid)

Upvotes: 0

Related Questions