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
Related
I'm trying to use print with .format() to create a table, but I'm getting a syntax error on:
print("|{:<40}|{:<40}|{:<40}|{:<40}|".format(*tableheads))
with tableheads being a predefined list of 4 arguments. The error I'm getting when I try to run is:
print("|{:<40}|{:<40}|{:<40}|{:<40}|".format(*tableheads))
^
SyntaxError: invalid syntax
For a while I thought I'd done something wrong, but I've looked through the documentation, and even tried it in the console where it worked perfectly. For some reason though, I'm getting a syntax error when I try to include it as part of a function.
I'm not sure what to do, and I feel like I must be missing something, but what?
Thanks in advance.
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).
Is it normal for me to use parenthesis when making a call to print in Python 2.7? I thought only 3 onwards needs to do this?
without the parenthesis I get a syntax error.
SyntaxError: invalid syntax
but with parenthesis, it works.
I was under the assumption that 2.7 doesn't need parenthesis to print?
Your impression is correct, it's not needed (unless of course you import print_function von __future__!). However, it's not prohibited either. print is followed by an expression, and (sys.version) is a valid expression as much as sys.version is. Note that (x) does not create a tuple containing x (that would be (x,)).
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.
Is Python IDLE consistent regarding syntax coloring (highlighting) of print?
I tested using IDLE 2.7.2. Sometimes it recognizes as a keyword. Sometimes as built-in ( such as list() or tuple() ). Both of them are, IMHO, correct; so IDLE is inconsistent?
Let me know your opinion.
KW
Added a screenshot 2012 04 01 18:27 KST (April fool's day but the shot is true.)
Link to my screenshot
How I made this:
Second print will be built-in from the beginning.
The last(=fourth) print is more tricky. First leave no blank line between the third and the fourth print. Then the fourth print is a keyword.
If I put insertion point (=cursor) at the end of the third print and press [Enter] key, the fourth print finally becomes a built-in.
Please let me know if I can have some more positive feedback now :)
Without examples or screenshots there isn't really much we can do here, but if you really want great Syntax Highlighting and error detection I'd recommend using an IDE like PyCharm. It is not open source but it is the best Python IDE I've tried.
This has been fixed: http://bugs.python.org/issue6528