Reputation: 21
i'm writing a phylogenetic tree program in python and i'm running into a small problem i've not encountered before, i have looked into it and nothing quite describes the problem i have, i get a syntax error from this code:
for x in range(len(matrix)):
# Print the label
print(seq0[x-1], end == ' ') if x > 0 else print( ' ', end == '')
the error says:
File "/home/brandon/OptimalAlignmentSensitive.py", line 206
print(seq0[x-1], end == ' ') if x > 0 else print( ' ', end == '')
and it points to the 't' in the second print statement. has anyone else had this problem? i don't understand what to do i have tried adding spaces and such but nothing helps.
Upvotes: 2
Views: 153
Reputation: 88977
Your issue is the use of the ternary operator. You are using 2.x, where print
is a statement - while the ternary operator only accepts expressions. Replace it with an if
statement:
for x in xrange(len(matrix)):
if x > 0:
print(seq0[x-1], end == ' ')
else:
print(' ', end == '')
This probably won't do what you want though. This will print a tuple
, while you probably wanted to use the keyword argument from 3.x's print()
function. This doesn't exist in 2.x, and instead, that functionality is produced by leaving a trailing comma (without brackets to construct a tuple).
for x in range(len(matrix)):
if x > 0:
print seq0[x-1],
else:
print " ",
In general, using the ternary operator to emulate a single line if
statement is a bad practice anyway, even where it does work. As stated in my comments, iteration by index is generally a bad idea, and can probably be removed, but that would require more insight into the operation at hand.
Upvotes: 1
Reputation: 6828
Try adding from __future__ import print_function
, and writing print(' ', end='')
with a single '=' in the print
function.
Upvotes: 4