Why does it say “Invalid Syntax” When I try to print something in Python? [duplicate]


Why does it say “Invalid Syntax” When I try to print something in Python? [duplicate]



This question already has an answer here:



When I try to run my Python code in Idle (on a Mac), is says 'invalid syntax' On my quotation marks. Does anyone know why?


from goto import goto, label
import random

tries = 0
r = int(100 * random.random()) + 1
label .guess

guess = int(raw_input("Please guess the number between 1 - 100: "))
if guess == r:
tries += 1
print "You are correct! It took you " + str(tries) + " tries!"

if guess > r:
print"Too high... Try again"
tries += 1
goto .guess

if guess < r:
print"Too low... Try again."
tries += 1
goto .guess
exit



Does any one know why this might not work? The line that has an error is this one:


print "You are correct! It took you " + str(tries) + " tries!"



It is the second quotation mark.



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





Which Python version are you running? If it is Python 3, print is a function and entails parentheses.
– coffeinjunky
Jun 29 at 21:36


print





I am using Python 3.7.0
– Brendan
Jun 29 at 21:37




1 Answer
1



Try:


print("You are correct! It took you " + str(tries) + " tries!")



In Python 3 and above, print statements are function calls, that is the input needs to be in parentheses. See e.g. http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html#the-print-function

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift