返回外部函数 [英] return outside function

查看:91
本文介绍了返回外部函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii我在Biopython中遇到以下错误:函数外部返回"(文件名..第26行) 下面是myfile的代码 请帮助

Hii I am getting the following error in Biopython: 'return' outside function (filename.. line 26) Below is the code of myfile PLEASE HELP

# 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对缩进敏感.如果您的代码缩进严重,它将无法正常工作.

Python is sensitive to indentation. If your code is badly indented, it won't work.

我惊人的谷歌搜索功能告诉我您已从

My amazing googling powers tell me you've taken your code from this page, where unfortunately the code isn't properly formatted either.

但是在这里,我付出了努力.不过,如果这失败了,我将不承担任何责任,因为我没有运行它,甚至没有在精神上运行.

But here, I took the effort. I'm not responsible if this will fail miserably though, because I didn't run it, not even mentally.

# 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

这篇关于返回外部函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆