为什么该脚本会出现语法错误?

时间:2019-04-16 16:57:30

标签: python bioinformatics

我从Youtube视频教程中编写了此脚本,目的是练习python中的生物信息学编码。当我尝试在Python 3.7中运行.py文件时,出现此错误print "number of g's " + str(g)

读取的.py文件和.txt文件位于我的桌面上的同一文件夹中。我正在使用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)

1 个答案:

答案 0 :(得分:1)

当我使用Python 2.7或Python 3.7运行脚本时,不会出现您提到的错误。尽管在使用 Python 3.7 print "number of g's " + str(g) SyntaxError: invalid syntax时会出现另一个错误,并且由于您在问题中提到了该错误,所以您应该对此进行更改:

print "number of g's " + str(g) //可在Python 2.7上使用

对此:

print("number of g's " + str(g)) //适用于Python 3.7