I have created a function named validate which checks if a sequence is DNA, RNA or something else. If it is DNA or RNA it prints a message that it is valid, otherwise that it's invalid (seqtype
is what I have named the type of biological sequence based on which the validation is done):
def validate (self):
if self.seqtype == "DNA" or self.seqtype == "RNA":
self.seqtype == self.seqtype
print ("The sequence is valid")
else:
print ("The sequence is invalid")
Now the user has to input a sequence. This input must be "passed" through the function validate and if the input is valid, add the input to a table, otherwise if it is invalid, print an error statement. In other words something like this:
if The sequence is valid:
CONTINUE TO THE NEXT TASK
elif The sequence is invalid:
PRINT AN ERROR MESSAGE TO THE USER
Can you please help me how to do that?
CodePudding user response:
def validate (self):
if self.seqtype == "DNA" or self.seqtype == "RNA":
self.seqtype == self.seqtype
print ("The sequence is valid")
self.next_task() #added, as question says that should do this here
else:
print ("The sequence is invalid")
or add to list, which the question says more specifically, here in but could be in the next_task or some add_valid method.
def validate (self):
if self.seqtype == "DNA" or self.seqtype == "RNA":
self.seqtype == self.seqtype
print ("The sequence is valid")
valid_seqs.append(self) #the list must be inited somewhere
else:
print ("The sequence is invalid")
CodePudding user response:
I think you would want to make validate()
a standalone function — not a class method — and call it when needed, such as before creating an instance of your class. This will avoid creating instances of the class with an invalid sequence.
Here's an example of what I am suggesting:
class Nucleotide:
def __init__(self, seqtype):
self.seqtype = seqtype
def validate(seqtype):
return seqtype in {"DNA", "RNA"}
while True:
seqtype = input('Enter sequence type: ("DNA" or "RNA"): ')
if not validate(seqtype):
print("The sequence is invalid, please try again.")
else:
print("The sequence is valid.")
molecule = Nucleotide(seqtype)
break # Go on to next task.
...