Help, creating a guessing game

I’m creating a guessing game and cant seem to get this code to work, I’m not sure what is wrong with it.

answer = ‘yes’;
console.log (“Please, think of a number between 1 and 100.”);
while answer == “yes”;
NumOfTry = 10;
NumToGuess = 50;
LimitLow = 1;
LimitHigh = 100;
while NumOfTry != 0:
try:
console.log (“I try: “,NumToGuess)
console.log (“Please type: 1 for my try is too high”)
console.log (” 2 for my try is too low”)
console.log (" 3 I guessed your number")
HumanAnswer = int (input(“So did I guess right?”))
if 1 < HumanAnswer > 3:
console.log (“Please enter a valid answer. 1, 2 and 3 are the valid choice”)
NumOfTry = NumOfTry + 1
if HumanAnswer == 1:
LimitHigh = NumToGuess
print ("Hmm, so your number is between ",LimitLow, "and ", LimitHigh)
NumOfTry = NumOfTry - 1
console.log (NumOfTry, “attempts left”)
NumToGuess = int (((LimitHigh - LimitLow)/2) + LimitLow)
if NumToGuess <= LimitLow:
NumToGuess = NumToGuess + 1
if LimitHigh - LimitLow == 2:
NumToGuess = LimitLow + 1
elif HumanAnswer == 2:
LimitLow = NumToGuess
console.log ("Hmm, so your number is between ",LimitLow, "and ", LimitHigh)
NumOfTry = NumOfTry - 1
console.log (NumOfTry, “attempts left”)
NumToGuess = int (((LimitHigh - LimitLow)/2) + LimitLow)
if NumToGuess <= LimitLow:
NumToGuess = NumToGuess + 1
if LimitHigh - LimitLow == 2:
NumToGuess = LimitLow + 1
elif HumanAnswer == 3:
console.log (“Woo hoo! I won”)
NumOfTry = 0
except:
break
else:
answer = input (‘Do you want to play again? (yes/no)’)

else:
console.log (“Thank you for playing. Goodbye”)

The real guessing game at this time is which language do you want to use for the program? JavaScript or Python? The above is a hybrid mixture, but still suitable for pseudo-code so don’t scrap it, Use it as a blueprint.

Thanks, I was also wondering how to get the code to work, if I have mixed it up i’m wondering what program I should change the code to :slight_smile: Thanks

It looks like mostly Python so if you are comfortable with that language then it’s the one to go with. Change all console.log() statements to `print(), and get rid of all the semi-colon syntax, for a start.

Thanks for your help, it all worked perfectly :smiley:

1 Like

Let us see what you have come up with, please. Thanks.

This is what I was able to derive…

from time import sleep
answer = 'y'
while answer == 'y':
  print ("Please, think of a number between 1 and 100.")
  sleep(5)
  NumOfTry = 10
  NumToGuess = 50
  LimitLow = 1
  LimitHigh = 100
  while NumOfTry > 0:
    try:
      print ("I try: %d" % NumToGuess)
      print ("Please type:\n 1 for my try is too high")
      print (" 2 for my try is too low")
      print (" 3 I guessed your number")
      HumanAnswer = int (input("So did I guess right? "))
      if 1 > HumanAnswer > 3:
        print ("Please enter a valid answer. 1, 2 and 3 are the valid choice")
        NumOfTry = NumOfTry + 1
      if HumanAnswer == 1:
        LimitHigh = NumToGuess
        print ("Hmm, so your number is between %d and %d" % (LimitLow, LimitHigh))
        NumOfTry -= 1
        print ("%d attempts left" % NumOfTry)
        NumToGuess = int (((LimitHigh - LimitLow)/2) + LimitLow)
        if NumToGuess <= LimitLow:
          NumToGuess += 1
        if LimitHigh - LimitLow == 2:
          NumToGuess = LimitLow + 1
      elif HumanAnswer == 2:
        LimitLow = NumToGuess
        print ("Hmm, so your number is between %d and %d" % (LimitLow, LimitHigh))
        NumOfTry = NumOfTry - 1
        print ("%d attempts left" % NumOfTry)
        NumToGuess = int (((LimitHigh - LimitLow)/2) + LimitLow)
        if NumToGuess <= LimitLow:
          NumToGuess = NumToGuess + 1
        if LimitHigh - LimitLow == 2:
          NumToGuess = LimitLow + 1
      elif HumanAnswer == 3:
        print ("Woo hoo! I won!")
        NumOfTry = 0
    except:
      break
  else:
    answer = input ('Enter `y` to play again. ')
print ("Thank you for playing. Goodbye")
================ RESTART: D:/Python35/users/guess_a_number.py ================
Please, think of a number between 1 and 100.
I try: 50
Please type:
 1 for my try is too high
 2 for my try is too low
 3 I guessed your number
So did I guess right? 1
Hmm, so your number is between 1 and 50
9 attempts left
I try: 25
Please type:
 1 for my try is too high
 2 for my try is too low
 3 I guessed your number
So did I guess right? 2
Hmm, so your number is between 25 and 50
8 attempts left
I try: 37
Please type:
 1 for my try is too high
 2 for my try is too low
 3 I guessed your number
So did I guess right? 3
Woo hoo! I won!
Enter `y` to play again. 
Thank you for playing. Goodbye
>>> 
answer = 'yes'
print ("Please, think of a number between 1 and 100. I am about to try to guess it in 10 tries.")
while answer == "yes":
    NumOfTry = 10
    NumToGuess = 50
    LimitLow = 1
    LimitHigh = 100
    while NumOfTry != 0:
        try:
            print("I try: ",NumToGuess)
            print ("Please type: 1 for my try is too high")
            print ("             2 for my try is too low")
            print ("             3 I guessed your number")
            HumanAnswer  = int (input("So did I guess right?"))
            if 1 < HumanAnswer > 3:
                print("Please enter a valid answer. 1, 2 and 3 are the valid choice")
                NumOfTry = NumOfTry + 1
            if HumanAnswer == 1:
                LimitHigh = NumToGuess
                print ("Hmm, so your number is between ",LimitLow, "and ", LimitHigh)
                NumOfTry = NumOfTry - 1
                print(NumOfTry, "attempts left")
                NumToGuess = int (((LimitHigh - LimitLow)/2) + LimitLow)
                if NumToGuess <= LimitLow:
                    NumToGuess = NumToGuess + 1
                if LimitHigh - LimitLow == 2:
                    NumToGuess = LimitLow + 1
            elif HumanAnswer == 2:
                LimitLow = NumToGuess
                print ("Hmm, so your number is between ",LimitLow, "and ", LimitHigh)
                NumOfTry = NumOfTry - 1
                print (NumOfTry, "attempts left")
                NumToGuess = int (((LimitHigh - LimitLow)/2) + LimitLow)
                if NumToGuess <= LimitLow:
                    NumToGuess = NumToGuess + 1
                if LimitHigh - LimitLow == 2:
                    NumToGuess = LimitLow + 1
            elif HumanAnswer == 3:
                print ("Woo hoo! I won")
                NumOfTry = 0
        except:
            break
    else:
        answer = input ('Do you want to play again? (yes/no)')

else:
    print ("Thank you for playing. Goodbye")

I just had to change the console.log to print and get rid of all the ;

And format your code so we code visualize it, right?

We still have one or more edge cases to deal with. This program does not solve for 1, at least not mine.

================ RESTART: D:/Python35/users/guess_a_number.py ================
Please, think of a number between 1 and 100.
I try: 50
Please type:
 1 for my try is too high
 2 for my try is too low
 3 I guessed your number
So did I guess right? 1
Hmm, so your number is between 1 and 50
9 attempts left
I try: 25
Please type:
 1 for my try is too high
 2 for my try is too low
 3 I guessed your number
So did I guess right? 1
Hmm, so your number is between 1 and 25
8 attempts left
I try: 13
Please type:
 1 for my try is too high
 2 for my try is too low
 3 I guessed your number
So did I guess right? 1
Hmm, so your number is between 1 and 13
7 attempts left
I try: 7
Please type:
 1 for my try is too high
 2 for my try is too low
 3 I guessed your number
So did I guess right? 1
Hmm, so your number is between 1 and 7
6 attempts left
I try: 4
Please type:
 1 for my try is too high
 2 for my try is too low
 3 I guessed your number
So did I guess right? 1
Hmm, so your number is between 1 and 4
5 attempts left
I try: 2
Please type:
 1 for my try is too high
 2 for my try is too low
 3 I guessed your number
So did I guess right? 1
Hmm, so your number is between 1 and 2
4 attempts left
I try: 2
Please type:
 1 for my try is too high
 2 for my try is too low
 3 I guessed your number
So did I guess right? 1
Hmm, so your number is between 1 and 2
3 attempts left
I try: 2
Please type:
 1 for my try is too high
 2 for my try is too low
 3 I guessed your number
So did I guess right? 3
Woo hoo! I won!
Enter `y` to play again. 
Thank you for playing. Goodbye
>>>