Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5850591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:09:43+00:00 2026-05-22T13:09:43+00:00

Hii I am getting the following error in Biopython: ‘return’ outside function (filename.. line

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T13:09:43+00:00Added an answer on May 22, 2026 at 1:09 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm getting the error while executing following code in MySQL : BEGIN DECLARE
Hi I'm using the following code but I'm getting an unexpected T_FUNCTION syntax error
Hi i'm getting an error when using this code Session(formatdate) = Left(drv.Row(booking_status), 10) Session(formatdate).ToString(dd-MMM-yyyy)
Hi I am getting the following errors reported by ELMAH on my asp.net mvc
hi i am getting the following exception while running my application and my applicationContext.xml
Hi i am using following code. NSString *quant = [NSString stringWithFormat:@%@,item.capacity]; NSString *metricUnit =
Hi I have the following code <ul id=sample-menu-1 class=sf-menu> <li class=current><a href=#a>main item <span
Hii all, I am trying to downaload large file in rails using send_data function
Hi I have shared the code here : http://jsfiddle.net/jTAZ4/ Why am i getting the
Hi I am getting the above error setting up my Zend application on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.