【问题标题】:return outside function [closed]返回外部函数[关闭]
【发布时间】:2011-05-19 09:40:07
【问题描述】:

嗨,我在 Biopython 中收到以下错误:'return' outside function (filename.. line 26) 下面是myfile的代码 请帮忙

# File Name RandonProteinSequences.py
# standard library
import os
import random

# biopython
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SeqRecord import SeqRecord
import Bio.writers.SeqRecord.fasta
from Bio import SeqIO
from sys import *

residueList1 = ["C","D","E","F","G","H","I"]
residueList2 = ["A","K","L","M","N","S"]
residueList3 = ["P","Q","R","T","V","W","Y"]
residueList4 = ["C","A","G","U"]
def getProteinSeqRecord(residue, seqcount):
    strSeq = ""
for i in range(0,100,1):
    index = random.randint(0, len(residue)-1)
    strSeq += residue[index]

sequence = Seq(strSeq, IUPAC.IUPACProtein)
seqRec = SeqRecord(sequence, id = 'randSeq' + str(seqcount), description= 'A random sequence using Amino acid residues.')
return seqRec

def getProteinSequence(residue):
    strSeq = ""
for i in range(0,100,1):
    index = random.randint(0, len(residue)-1)
strSeq += residue[index]

sequence = Seq(strSeq, IUPAC.IUPACProtein)
return sequence

def randomProteinSeqRecord(index):
    if(index%2)==0:
        return getProteinSeqRecord(residueList1, index)
    elif(index%3)==0:
        return getProteinSeqRecord(residueList2, index)
    else:
        return getProteinSeqRecord(residueList3, index)

#information
print '--- This is python based program to generate random sequences ---'
print '--- Provide number of random sequences to generate. Default 10 ---'
print '--- Inorder to save to a file provide file path or filename ---'
print '--- If none or invalid filepath is provided then results will be displayed to console ---'
print '--- The file will be created in fasta format ---'
print

filepathProvided = False
#raw_input received the user input as string
try:
    filepath = raw_input('Enter filepath to save sequences ... ')
    filepath = filepath + '.fasta'
    handle = open(filepath, "w")
    handle.close()

    filepathProvided = True
except IOError:
    print 'Invalid or No File provided will print results to console'
print
ranSeqCount = 10
try:
    ranSeqCount = int(raw_input('Enter number of random sequences to generate ... '))
except ValueError:
    ranSeqCount = 10
pass

if(filepathProvided):
    handle = open(filepath, "w")

if(filepathProvided):
    fasta_writer = Bio.writers.SeqRecord.fasta.WriteFasta(handle)
else:
    fasta_writer = Bio.writers.SeqRecord.fasta.WriteFasta(stdout)
print 'Sequence Count : '
print ranSeqCount

for i in range(0,ranSeqCount,1):
    fasta_writer.write(randomProteinSeqRecord(i+1))
if(filepathProvided):
    handle.close()
print 'File created at : ' + filepath

print
raw_input('Press any key to exit ...')
print

【问题讨论】:

  • 看来你复制粘贴错了
  • 尽管问题发布者显然可以进行一些研究以找到解决方案,但我认为这个问题不值得投反对票,因为它对 python 新手非常有用。整个空白问题,加上从/到各种来源的复制粘贴的格式丢失可能会非常令人困惑
  • 确实如此。我既没有投反对票,也没有投赞成票。但我不想给出一个严肃的答案,因为 OP 显然没有花太多时间学习 python 的基础知识。

标签: python biopython


【解决方案1】:

Python 对缩进很敏感。如果您的代码缩进严重,它将无法工作。

我惊人的谷歌搜索能力告诉我,您从this page 获取了您的代码,不幸的是,该代码的格式也不正确。

但在这里,我付出了努力。如果这会惨败,我不负责,因为我没有运行它,甚至没有精神上的。

# File Name RandonProteinSequences.py
# standard library
import os
import random

# biopython
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SeqRecord import SeqRecord
import Bio.writers.SeqRecord.fasta
from Bio import SeqIO
from sys import *

residueList1 = ["C","D","E","F","G","H","I"]
residueList2 = ["A","K","L","M","N","S"]
residueList3 = ["P","Q","R","T","V","W","Y"]
residueList4 = ["C","A","G","U"]
def getProteinSeqRecord(residue, seqcount):
    strSeq = ""
    for i in range(0,100,1):
        index = random.randint(0, len(residue)-1)
        strSeq += residue[index]

    sequence = Seq(strSeq, IUPAC.IUPACProtein)
    seqRec = SeqRecord(sequence, id = 'randSeq' + str(seqcount), description= 'A random sequence using Amino acid residues.')
    return seqRec

def getProteinSequence(residue):
    strSeq = ""
    for i in range(0,100,1):
        index = random.randint(0, len(residue)-1)
        strSeq += residue[index]

    sequence = Seq(strSeq, IUPAC.IUPACProtein)
    return sequence

def randomProteinSeqRecord(index):
    if(index%2)==0:
        return getProteinSeqRecord(residueList1, index)
    elif(index%3)==0:
        return getProteinSeqRecord(residueList2, index)
    else:
        return getProteinSeqRecord(residueList3, index)

#information
print '--- This is python based program to generate random sequences ---'
print '--- Provide number of random sequences to generate. Default 10 ---'
print '--- Inorder to save to a file provide file path or filename ---'
print '--- If none or invalid filepath is provided then results will be displayed to console ---'
print '--- The file will be created in fasta format ---'
print

filepathProvided = False
#raw_input received the user input as string
try:
    filepath = raw_input('Enter filepath to save sequences ... ')
    filepath = filepath + '.fasta'
    handle = open(filepath, "w")
    handle.close()

    filepathProvided = True
except IOError:
    print 'Invalid or No File provided will print results to console'
print
ranSeqCount = 10
try:
    ranSeqCount = int(raw_input('Enter number of random sequences to generate ... '))
except ValueError:
    ranSeqCount = 10
pass

if(filepathProvided):
    handle = open(filepath, "w")

if(filepathProvided):
    fasta_writer = Bio.writers.SeqRecord.fasta.WriteFasta(handle)
else:
    fasta_writer = Bio.writers.SeqRecord.fasta.WriteFasta(stdout)
print 'Sequence Count : '
print ranSeqCount

for i in range(0,ranSeqCount,1):
    fasta_writer.write(randomProteinSeqRecord(i+1))
if(filepathProvided):
    handle.close()
print 'File created at : ' + filepath

print
raw_input('Press any key to exit ...')
print

【讨论】:

  • +1 为你惊人的谷歌搜索能力:)
  • 谢谢....这有效...虽然现在它说没有名为 writers.SeqRecord.fasta 的模块我可以从哪里下载这个模块...实际上我的 Python 很弱跨度>
  • @Hemant0285 This page 表示它未被使用、未记录、已弃用。这意味着它可能在此期间被删除。恐怕您找到的这段代码一定很旧,需要更新。
【解决方案2】:

Python 依靠缩进来确定函数(和其他块结构)在哪里结束。据推测,导致您的错误的函数应该如下所示:

def getProteinSeqRecord(residue, seqcount):
    strSeq = ""
    for i in range(0,100,1):
        index = random.randint(0, len(residue)-1)
        strSeq += residue[index]

    sequence = Seq(strSeq, IUPAC.IUPACProtein)
    seqRec = SeqRecord(sequence, id = 'randSeq' + str(seqcount), description= 'A random sequence using Amino acid residues.')
    return seqRec

【讨论】:

  • 这行得通....现在它在第 35 行显示了同样的错误....感谢上述更正//缩进的代码....
  • 在第 35 行解决了同样的问题...效果很好.. 只是我在下面提到的模块丢失错误
【解决方案3】:

你的意图错了

【讨论】:

  • 这个答案是完全正确的,虽然并不冗长。所以它不应该被否决。
  • 得到这个错误:: Traceback (最近一次调用最后一次): File "C:\Users\Hemant\Desktop\RandonProteinSequences.py", line 10, in import Bio.writers.SeqRecord .fasta ImportError: No module named writers.SeqRecord.fasta
  • @vikingosegundo:“完全正确”和“有用”是两个不同的东西。告诉人们在第一行和最后一行之间的某个地方有空格错误实际上是没有用的,尤其是对于初学者。
  • 有了信息,缩进是错误的,OP 有足够的信息来查看 python.org、google 和 SO。所以这不是一个无用的答案。
  • 兄弟们,请不要讨论谁对谁错,,,而是请帮助我......这个错误回溯(最近一次通话最后):文件“C:\用户\ Hemant\Desktop\RandonProteinSequences.py",第 10 行,在 中 import Bio.writers.SeqRecord.fasta ImportError: No module named writers.SeqRecord.fasta
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 2015-11-21
  • 2019-11-25
相关资源
最近更新 更多