Jesper
Jesper

Reputation: 141

loop prints a error message multiple times

edited for clarity. When a loop prints an error message multiple times, it is usually caused by poor control flow. In this case, adding a Breakafter print solved the problem.

Following a simple loop structure with some control flow, is generally a good starting point.

for ...:
    if ...:
        print ...
        break

input_seq = "";

#raw_input() reads every input as a string
input_seq = raw_input("\nPlease input the last 12 nucleotide of the target sequence 
before the PAM site.\n\(The PAM site is by default \"NGG\"\):\n12 nt = ")

#print "raw_input =", input_seq
for bases in input_seq:
   if not (bases in "ACTGactg"):
       print "\nYour input is wrong.  Only A.T.C.G.a.t.c.g are allowed for 
       the input!\n\n";
       break

Upvotes: 0

Views: 936

Answers (4)

ma08
ma08

Reputation: 3734

Use break. I am using regular expressions to avoid using a for loop.

input_seq = ""
import re

while True:

  #raw_input() reads every input as a string
  input_seq = raw_input("\nPlease input the last 12 nucleotide of the target sequence 
   before the PAM site.\n\(The PAM site is by default \"NGG\"\):\n12 nt = ")

  #print "raw_input =", input_seq
  if len(input_seq)==12:
    match=re.match(r'[ATCGatcg]{12}',input_seq)
    if match:
      break
    else:
      print "\nYour input is wrong.  Only A.T.C.G.a.t.c.g are allowed for the input!\n\n"
      continue
  else:
    print "\nYour input should be of 12 character"

Upvotes: 1

f.rodrigues
f.rodrigues

Reputation: 3587

Place a break after the print:

for ...:
    if ...:
        print ...
        break

Upvotes: 0

shaktimaan
shaktimaan

Reputation: 12092

Using a flag to check if there were atleast one wrong input is one way to do it. Like this:

invalid = False
for bases in input_seq:
    if not (bases in "ACTGactg"):
        invalid = True

if invalid:
    print "\nYour input is wrong.  Only A.T.C.G.a.t.c.g are allowed for the input!\n\n";

You could alternately use a break statement as soon as you find the first wrong input.

Upvotes: 0

Matthew Weis
Matthew Weis

Reputation: 40

Add a break after your print statement. That will terminate your loop.

Upvotes: 0

Related Questions