Something like this:
list1 = ['A', 'C', 'G', 'T']
repeat = True
while repeat:
a = input("Please enter your gene sequence: ")
repeat = any([char.upper() not in list1 for char in a])
This [char.upper() not in list1 for char in a]
is known as list comprehension in Python and allows you to create a new list by iterating over the characters in your string a
. The value added for each character in a
is char.upper() not in list1
which is a boolean value, checking if that character does not exist in list1
. At the end you should have a new list [False, True, False]
and using any()
higher-order function you check if in that list there is any (at least one) True value (i.e., if there is at least one True value it means there is a character in the string that is not in list1
).
For a = "ABC"
the list comprehension [char.upper() not in list1 for char in a]
yields to [False, True, False]
and any([False, True, False]) = True
.