循环多次打印错误消息

时间:2014-07-31 18:02:54

标签: python bioinformatics

我对python有点问题 - 答案已发布

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";

4 个答案:

答案 0 :(得分:1)

使用break。 我使用regular expressions来避免使用for循环

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"

答案 1 :(得分:0)

打印后放置break

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

答案 2 :(得分:0)

在print语句后添加break。这将终止你的循环。

答案 3 :(得分:0)

使用标志来检查是否存在至少一个错误的输入是一种方法。像这样:

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";

您可以在找到第一个错误输入后立即使用break语句。