0

I wrote this script from a Youtube video tutorial to practice coding in python for bioinformatics. When I try to run the .py file in Python 3.7, I get this error print "number of g's " + str(g).

The .py file and .txt file it reads is in the same folder on my desktop. I'm using Windows.

# First python program.
# GC content

gene = open("OCA2.txt", "r") 

# set values to 0
g=0;
a=0;
c=0;
t=0;

# skip the first header line
gene.readline()

for line in gene:
    line = line.lower()
    for char in line:
        if char == "g":
            g+=1
        if char == "a":
            a+=1
        if char == "c":
            c+=1
        if char == "t":
            t+=1

print "number of g's " + str(g)
print "number of c's " + str(c)
print "number of a's " + str(a)
print "number of t's " + str(t)


# 0. = convert to floating point
gc = (g+c+0.) / (a+t+c+g+0.)

print "gc content: " + str(gc)
denis_lor
  • 6,212
  • 4
  • 31
  • 55
Zadley
  • 21
  • 1
    Which line is the error on? – Barmar Apr 16 '19 at 16:58
  • 2
    `print` is a function in 3.x+ – Austin Apr 16 '19 at 16:59
  • 2
    Because it isn't *Python **3*** compatible (only *Python 2*). E.g. *print* is no longer a statement, but a function (but that's a different error). – CristiFati Apr 16 '19 at 16:59
  • @CristiFati Why would that cause this error? This error message is about an incorrect `\ ` character. – Barmar Apr 16 '19 at 17:00
  • @Barmar: right, I didn't read the error message. – CristiFati Apr 16 '19 at 17:02
  • You would get this error if you wrote `gc = (g+c+0.) \ (a+t+c+g+0.)` instead of `gc = (g+c+0.) / (a+t+c+g+0.)`. – Barmar Apr 16 '19 at 17:03
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Python 2.7 does not yield your syntax error. – Prune Apr 16 '19 at 17:07
  • @Prune did you meant Python 3.7? – denis_lor Apr 16 '19 at 17:07
  • I didn't say "3.7". My copy above says "2.7". Also, when I fix the input problem (to a file that does exist), the program runs just fine with Python 2.7. – Prune Apr 16 '19 at 17:14
  • @Prune The question says that he is running the script under python 3.7 and hence we should try the code in python 3.7. As I did and I gave an answer showing to use print with `()` – denis_lor Apr 16 '19 at 17:23

1 Answers1

1

The error you mentioned doesn't come up when I run the script either with Python 2.7 or Python 3.7. Although another error comes when using Python 3.7 print "number of g's " + str(g) SyntaxError: invalid syntax and since you mentioned it in the question you should change this:

print "number of g's " + str(g) // works on Python 2.7

to this:

print("number of g's " + str(g)) // works on Python 3.7

denis_lor
  • 6,212
  • 4
  • 31
  • 55