I have been learning Python on Codecademy.com and transferred a project that worked on the site (Battle Ship for those who used the site) and now I'm getting a syntax error with part of my code. On the site it runs smoothly.
The problem is when I take a list of strings and use join on them to display the game board.
result = " ".join(row)
print result
It is giving the error on the print result. On the site, the code was one line but I broke it down to see what was giving the error. Currently I have python 3.3 installed. Could something have changed between the version of Python interpreters that may have cause this?
In Python 3.x, print is a function, so try:
result = " ".join(row)
print(result)
you can use the following in python 2,
print "%s." %" ".join(row)
Alternatively, use the following in python 3,
print(*row, sep=' ')
Related
s = input("enter something:")
print(s)
d = input("enter something else")
print(d)
so once I input a string.. the program does not proceed.. nor does it print the string. (i'm a noob at python rn)
[I'm using sublimeText3 + Anaconda]
Check the screenshot out, it'd be more helpful
Are you running this on Python 2 or Python 3? Actually, with Python3, in terminal, your code runs fine to me. To check Python version, just type in terminal:
python --version
if it yields something like Python 2.7, try to run your code executing python3 instead. Otherwise, consider using raw_input() :)
EDIT: as others pointed out, it could something glitchy with your IDE. Try with terminal first!
I am always getting an error message stating "syntax error" when I ask it to print a simple sentence. I do not know why, I am presently working on an AI. This is the code for which I am getting the error message.
print ("what is your name ?")
The problem is not in the print ("what is your name ?") line.In Python 2.7 or Python 3, works correctly. you may have to check syntax of other lines of your code above to the print statement. you might have forgotten to close brackets or something else.
It looks like you're using Python 2.7. In al python versions < 3, the print function doesn't need parentheses. Either install python 3 or use print like this: print "what is your name ?". Also, in Python 3, there shouldn't be a space between print and (...).
So I have just started learning to use Python, and I am getting a syntax error.
Following the book I am using , here is a simple if statement, followed by a print statement that happens regardless of the if statement.
name = "Doug"
if name == 'Doug' :
print "Hello, Doug!"
print "How are you today?"
The expected output is:
Hello Doug!
How are you today?
if name != Doug, then the output should be
How are you today?
I've done simple ifs a thousand times in C++ and Java, but with brackets. For some reason, the final print comes back with a syntax error.
I am using Python 2.7.8, not Python 3, and using print or print() gives me the same result.
EDIT:
No amount of Newlines in the interpreter version worked, however running the script in a .py file worked flawlessly. For some reason , my book failed to mention this.
Your code works for me if I put it in a .py file and run the file through the interpreter. E.g. python hello.py. If I run the python interpreter interactively, however, then I can reproduce a syntax error at the second print statement.
I think this is just a quirk of interactive mode. I can make it work in interactive mode, too, by putting an extra newline between the two print statements. For what it's worth, the interactive-mode prompting makes me think that it doesn't recognize the end of the if statement until I type that extra newline after it (otherwise, another statement in the if block might follow).
I am very new to programming and I'm starting out with Python. I tried to look up my question here but didn't really find anything.
I'm trying to work a very simple print command but I'm getting an error for some reason that I don't understand.
last = 'smith'
middle = 'paul'
first = 'john'
print(first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')
According to the answer in the book, this should be right, but every time I try to run it, I get an error with the 'sep':
print(first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')
^
SyntaxError: invalid syntax
Can someone tell me what I'm doing wrong. for what it's worth I'm using PyScripter.
[EDIT]
Thanks for that. I found out that I'm using Python 2.7.3 instead of 3.3. So I looked up the manual to see how the separator works. It seems to me that the only difference is with the square bracket. The manual describes the print function as :
print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout])
So I changed my print command and added the square bracket:
print ([first.capitalize(),middle.capitalize(),last.capitalize()] [, sep='\t'])
but unfortunately this doesn't work either as I get an error that highlights the square brackets around sep='\t'. Even when I take the brackets out, the error doesn't go away.
I'm not sure what I'm doing wrong, it seems like it should be very simple.
You aren't actually using Python 3, you just think you are. Try:
import sys
print(sys.version)
and see what comes out. The Python 2 print ... statement (not print(...) function in Python 3) interprets this as
print (first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')
which is trying to print a tuple with a keyword argument, thus the syntax error on sep
So I have the following code:
f = open('input.txt', 'r')
text = f.read()
data = text.split()
print data
print '<HTML>\n <HEAD>\n </HEAD>\n <BODY>\n <table border="1">\n'
for x in data:
print ' <tr>' + x + '<tr>'
print'</table>\n </BODY>\n</HTML>'
before I tried to installed iPython, it was working with the default Python shell.
But after I installed distribute and pyreadline and then iPhython, the code won't stop giving me syntax errors, as if not a single variable would work, not sure if there is something about python initialization/declaration that I have missed or if something went wrong with doing stuff on the console, but it certainly is driving me crazy and I need to fix it.
P.S. I use Windows 8
Edit:
Was asked for the errors I get, here are screenshots, since I do not get any very specific text-like errors.
In this second one, I edited the code several times to test different things,
hence why I get error for different variables.
Even the simplest of things would give me an error.
P.S.2. I just tried print 'hey' and it gave me the same error, not recognizing the ' token.
Looks like whatever you installed is using Python 3.x, and your code was written for Python 2.x.
In Python 3.x, print is now a function, not a keyword, so you'll have to change all the lines like...
print data
...to...
print(data)
If you need to retain Python 2.x compatibility, add the line...
from __future__ import print_function
...at the top of the code.