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

Anthony Okutsu okutsu at pdx.edu
Sat Oct 1 05:55:16 CEST 2011


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.")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110930/4a42bbfd/attachment.html>


More information about the Tutor mailing list