Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude inputs from a specific input?

Tags:

python

For example, I want to exclude inputs other than "G,T,A and C" from an input. How do I do this?

So, this is what I'm working on right now.:

a=input('What is your DNA sequence?: ')

I need to exclude inputs other than "G,T,A and C" from this.

like image 756
user14291648 Avatar asked Mar 13 '26 05:03

user14291648


2 Answers

You could use regular expressions for filtering. The most simple example would be:

import re

x = input("What is your DNA sequence?: ")
r = re.compile('^[CAGT]+$')

while not r.match(x):
    print ("DNA sequence '" + x + "' invalid")
    x = input("What is your DNA sequence?:")

Here regexp ^[CAGT]+$ means that input should match characters C, A, G, or T, which are repeated (+) from the beginning of the string (^) to its end ($).

like image 133
Alexandra Dudkina Avatar answered Mar 15 '26 19:03

Alexandra Dudkina


You can try:

tokens = {'G', 'T', 'A', 'C'}

while True:
    inp = input('>>')

    if inp == 'quit':
        break

    if not set(inp).issubset(tokens):
        print('invalid tokens')
    else:
        print('valid')

Sample test cases:

>>ATGCCTGGGTACTAA
valid
>>AGTweddc
invalid tokens
like image 23
Girish Hegde Avatar answered Mar 15 '26 18:03

Girish Hegde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!