0

Below is my code for a barcode generator for dna sequencing. When I enter nothing for the mingc and max gc, the interpreter does not give any results. Did I use the correct format, ex. if mincg ='': etc.

import random
nucl_list = ['A', 'C', 'G', 'T']

length = 10

print '\nEnter desired GC content range in percentages (i.e. 50 ->50%)'

# ask for desired GC content range
mingc = raw_input("Minimum GC content (default is 40):")
if mingc == '':
    mingc = 40
else:
    mingc = float(mingc) / 100

maxgc = raw_input("Maximum GC content (default is 60):")
if maxgc == '':
    maxgc = 60
else:
    maxgc = float(maxgc) / 100

def gc_cont(barcode):
    gc = 0.0
    for base in range(length):
        if barcode[base] == 'C' or barcode[base] == 'G':
            gc += 1
        else:
            gc += 0
    cont = gc / length
    return cont

barcode = ''
while barcode == '':
    for i in range(length):
        barcode += random.choice(nucl_list)
        #print barcode
    if maxgc >= gc_cont(barcode) >= mingc:
        bar_code = barcode
        print bar_code
    else:
        barcode = ''`enter code here`
4
  • Hint: what is the output of print repr(mingc) after mingc = raw_input(...)? Commented May 9, 2015 at 20:47
  • exactly what I expected it to be, ('')... But for some reason the code only works if I enter "40". When I leave it blank, it doesn't work Commented May 9, 2015 at 20:52
  • 1
    @DanielMandel: And why is it that it doesn't work? After the if statement, what's mingc when you enter 40, and what's in it when you leave it blank? Just keep looking for places to add print statements (or, better, use the debugger) to figure out where it first goes wrong. Then, instead of having to look at your whole program and guess what might be wrong, you can look at the one line where things go wrong and figure it out. Commented May 9, 2015 at 20:53
  • Daniel, if you enter nothing mingc = 40 gets executed; if you enter 40, mingc = 40 / 100 gets executed. Commented May 9, 2015 at 20:56

1 Answer 1

1

You did use the right format here (although it's often more Pythonic to use if not mingc: than if mingc == '':).

If you want to see that for yourself, you can put a print "inside if" in the if statement, or print repr(mingc) to verify that it shows what you expected, or just print mingc after the if/else to see that the code you expected was executed, or run it in the debugger. It's worth learning how to do that.

But anyway, that's not your problem here. Your problem is that you put the wrong code inside the if body. Compare:

if maxgc == '':
    maxgc = 60
else:
    maxgc = float(maxgc) / 100

So, if the user types 60, you're going to set maxgc to float(60) / 100, or 0.6.

But if the user just hits enter to get the default, he doesn't get 0.6, he gets 60.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks. How would I run the code in the debugger? Where is the debugger?
@DanielMandel: pdb is the built-in debugger. If you're using a graphical IDE, it may have its own, easier-to-use wrapper around the pdb. If not, you have to learn the command interface. Which isn't that hard, but the docs aren't great unless you've used some debugger for some language in the past, so you may want to google for a better tutorial.
I use pycharm so it usuallt debugs for me
@DanielMandel: I don't know PyCharm that well, but I'm pretty sure it has a way to set breakpoints just by clicking on the line you want to stop at, run the program under the debugger, inspect the values of different values when it stops at the breakpoint, etc. I'm sure the PyCharm docs explain how to do all that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.