2 Replies - 287 Views - Last Post: 14 February 2013 - 01:05 PM Rate Topic: -----

#1 AbbKazPow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-February 13

Weird Syntax Error

Posted 14 February 2013 - 12:49 PM

This code isn't working...
For some reason there's a syntax error where I've highlighted with comments
from random import randrange
print """This is going to be the hardest game ever.
You, the lowly player, have to think of a random whole number between 1 and 100.
I, the supreme computer, will guess the number at random."""
compGuess = randrange(100)+1
nowGuess = compGuess
tries = 1
def repeatFunc():
    global highLow
    highLow=raw_input("My guess was: "+str(compGuess)""". Is this too high or too low?
Or are you too scared to tell me the truth? Tell me either "high", "low" or "correct" exactly the way I have told you.""")
repeatFunc()
while highLow != "correct":
	if highLow == "high":
		while compGuess <= nowGuess : # the syntax error is at the nowGuess on this line
			compGuess=randrange(100)+1
		tries += 1
	elif highLow == "low":
		while compGuess >= nowGuess:
			compGuess = randrange(100)+1
		tries += 1
	repeatFunc()
print "Ha ha. I have beat you. Of course, it only took me", tries, "tries. Newbie."


Can you please tell me why there is a syntax error?
This is really annoying me.

Is This A Good Question/Topic? 0
  • +

Replies To: Weird Syntax Error

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Weird Syntax Error

Posted 14 February 2013 - 01:00 PM

Dont use global variables. Just return the number from the function and assign it to a variable.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,019
  • Joined: 12-December 12

Re: Weird Syntax Error

Posted 14 February 2013 - 01:05 PM

If you are using Python 3 then the print function requires brackets:

print("Print this.")

Although you can use triple-quotes, like this
print("""Print this.""")

this seems unnecessary and confusing. Most would reserve these triple quotes for docstrings.

def my_func():
    """These are docstrings, comments about the function."""

Your raw_input() use in particular is mis-typed, not finished correctly. Perhaps you intended """Is this too high or ..""" to be a comment? In which case, finish your input with a closing bracket and use the hash-sign for your comment.

ARE YOU READING A TUTORIAL??

View Postdarek9576, on 14 February 2013 - 01:00 PM, said:

Dont use global variables. Just return the number from the function and assign it to a variable.

While this is good advice it doesn't attempt to resolve the OPs stated problem - the syntax error(s).

This post has been edited by andrewsw: 14 February 2013 - 01:06 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1