[Tutor] Can't figure out the syntax error!

Joel Goldstick joel.goldstick at gmail.com
Mon Oct 3 18:50:41 CEST 2011

On Fri, Sep 30, 2011 at 11:55 PM, Anthony Okutsu <okutsu at pdx.edu> wrote:

> I'm writing a program in which I input a number and the computer has to
> guess it. Every time I try to run the program it highlights the 'while' at
> the beginning of my loop and says 'invalid syntax' but I have no idea why.
> I'll paste a copy of my program below. Why is there a syntax error in my
> while loop?
> import random
> print("\tCan you beat the machine?\n\n")
> a = 0
> b = 101
> tries = 1
> guess = random.randint(a+1, b-1)
> number = int(input("Choose a number between 1 and 100 for CPU to guess:")
> while guess != number:
>     if guess < number:
>         a = guess
>     else:
>         b = guess
>     print(guess)
>     tries += 1
>     guess = random.randint(a+1, b-1)
> print("Your number is... ", guess, "!!!")
> print("And it only took me", tries, "tries. Can you beat me?")
> input("\n\nPress enter key to exit.")
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> I'm not sure from  you email what is wrong with the while statement.  You
should actually copy the traceback and place it in your message so that we
can see what is going wrong.

But you have some other problems.  You keep getting a new random number in
your while loop.  You should just get the random number at the beginning,
and after each incorrect guess ask for your input again.  So, fix that, then
run the program and send us back your traceback with the code

Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111003/60dc050c/attachment.html>

More information about the Tutor mailing list