10 Replies - 378 Views - Last Post: 01 May 2013 - 07:08 AM Rate Topic: -----

#1 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Code for all characters from inout must be found in a list

Posted 30 April 2013 - 08:10 AM

Hey guys this is my code, I'm very new to this but I am trying to work out a code that takes DNA code and converts it to mRNA then converts it to the amino acid sequence. I can't seem to find a way to make sure the user input is only one of the values found in the nucleotides list, but they may enter hundreds or thousands of As,Ts,Gs,Cs, or Us. So I need to be able to split their input into single letters then check that all of the letters are in list nucleotides.Can someone give me a hint?

#User inputs the code then a function decides whether the input is a valid genetic code"

DNA_Code=input( 'Please enter your gentic code of interest:' )

nucleotides=('A','T','G','C', 'U')

def dna_slice ():
    if DNA_Code.isalpha:
        UpperCaseCode=DNA_Code.upper()
        list(UpperCaseCode)
        if all(nucleotides):
            print('great')

        else:
            list(UpperCaseCode) and not all(nucleotides)
            print('no')
dna_slice()


I get the great statement whether I input ATTGC or ATTGO, it doesn't seem to check all of the characters in the input string.

Thanks for the help guys.

This post has been edited by modi123_1: 30 April 2013 - 08:10 AM
Reason for edit:: fixed botched code tag


Is This A Good Question/Topic? 0
  • +

Replies To: Code for all characters from inout must be found in a list

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7576
  • View blog
  • Posts: 12,729
  • Joined: 19-March 11

Re: Code for all characters from inout must be found in a list

Posted 30 April 2013 - 08:19 AM

You sure you want to put all the nucleotides in one list? Wouldn't you want to recognize DNA nucleotides in one list, and RNA in the other? This would allow for easy transciption, among other things.
Was This Post Helpful? 0
  • +
  • -

#3 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Re: Code for all characters from inout must be found in a list

Posted 30 April 2013 - 08:25 AM

View Postjon.kiparsky, on 30 April 2013 - 08:19 AM, said:

You sure you want to put all the nucleotides in one list? Wouldn't you want to recognize DNA nucleotides in one list, and RNA in the other? This would allow for easy transciption, among other things.



I wanted to put them all in the same list because once I figure out the aforementioned problem I am going to have it convert everything to mRNA which I'm sure you know contains the Uracil nucleotide rather than the Thymine. When converting to mRNA I will have to convert the T to a U. I wanted them all in the same list for now because I want the user to be able to also input prokaryotic RNA, where there is no conversion without having to tell the user that they have inputted RNA instead of DNA.

Is it still more useful to have seperate lists knowing that?
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Code for all characters from inout must be found in a list

Posted 30 April 2013 - 08:27 AM

User is asked to reenter input if it contains prohibited characters (code is python 3).
NUCLEOTIDES=('A','T','G','C', 'U')
bad_input = True
while bad_input:
    bad_input = False
    DNA_Code=input( 'Please enter your genetic code of interest:' ).upper()
    if any(char not in NUCLEOTIDES for char in DNA_Code):
        print("Input must be composed of only A/T/G/C/U")
        bad_input = True

-Mek
Was This Post Helpful? 1
  • +
  • -

#5 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Re: Code for all characters from inout must be found in a list

Posted 30 April 2013 - 08:29 AM

I really just want to learn python and I've gone over the basic stuff so I figure the only way to really learn it now is to come up with program ideas that aren't too difficult and try to create them.
Was This Post Helpful? 0
  • +
  • -

#6 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Code for all characters from inout must be found in a list

Posted 30 April 2013 - 08:35 AM

your previous:
if all(nucleotides):
always evaluated to true because all elements of it are non-empty strings. You weren't actually looking at if all the characters in your input were a part of it. Knowing what and how things evaluate to booleans is a simple, but extremely important part of learning python.

-Mek
Was This Post Helpful? 1
  • +
  • -

#7 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Re: Code for all characters from inout must be found in a list

Posted 30 April 2013 - 09:33 AM

View PostMekire, on 30 April 2013 - 08:27 AM, said:

User is asked to reenter input if it contains prohibited characters (code is python 3).
NUCLEOTIDES=('A','T','G','C', 'U')
bad_input = True
while bad_input:
    bad_input = False
    DNA_Code=input( 'Please enter your genetic code of interest:' ).upper()
    if any(char not in NUCLEOTIDES for char in DNA_Code):
        print("Input must be composed of only A/T/G/C/U")
        bad_input = True

-Mek



I don't fully understand the bad_input=True, then while bad_input:, bad_input = False, then bad_input=True again at the bottom of the code? Can you try to explain what this means in plain english for me please? Thank you.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7576
  • View blog
  • Posts: 12,729
  • Joined: 19-March 11

Re: Code for all characters from inout must be found in a list

Posted 30 April 2013 - 09:38 AM

Better if you try to walk through it for yourself. The logic may be new to you, but you can figure it out.

What happens in the while loop? Under what circumstances does it happen? What makes those circumstances change?
Was This Post Helpful? 1
  • +
  • -

#9 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Re: Code for all characters from inout must be found in a list

Posted 01 May 2013 - 06:13 AM

View Postjon.kiparsky, on 30 April 2013 - 09:38 AM, said:

Better if you try to walk through it for yourself. The logic may be new to you, but you can figure it out.

What happens in the while loop? Under what circumstances does it happen? What makes those circumstances change?


Well I've wrestled with this for a while. To me it seems like the while loop is set to while True.Then it asks for input and if any character is not found in the list of NUCLEOTIDES it sets it to True, which should start the loop over.

I would think the last line should set it to false so that it then breaks from the loop. And if the any() function is checking all characters why does it need to be a loop in the first place? I feel like a loop would check one character at a time and this doesn't seem to do that. I guess I need to get back to the books
Was This Post Helpful? 0
  • +
  • -

#10 Deaven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-April 13

Re: Code for all characters from inout must be found in a list

Posted 01 May 2013 - 06:20 AM

This works fine just like this
NUCLEOTIDES=('A','T','G','C', 'U')
DNA_Code=input( 'Please enter your genetic code of interest:' ).upper()
if any(char not in NUCLEOTIDES for char in DNA_Code):
    print("Input must be composed of only A/T/G/C/U")
    
else:
    print ('Anylizing')



The while loop is throwing me for a loop
Was This Post Helpful? 0
  • +
  • -

#11 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Code for all characters from inout must be found in a list

Posted 01 May 2013 - 07:08 AM

Deaven said:

The while loop is throwing me for a loop
The while just makes it so the user gets asked again for input on error rather than the program just failing.

First, bad_input is set to True. Our while loop will continue to run while bad_input is True. In the body of the loop bad_input is immediately set to False. Then we check that the characters in the input are all in the acceptable characters (or rather we check if any are not, because this is faster). If forbidden characters are found bad_input is set back to True so that the loop runs again. If no forbidden characters are found then bad_input stays False and the loop exits.

The any line:
if any(char not in NUCLEOTIDES for char in DNA_Code)
is just an easier way of writing this:
for char in DNA_Code:
    if char not in NUCLEOTIDES:
        #do stuff
        break
It checks all letters (until a letter fails).

You could also look at comparing sets (python 3 code):
NUCLEOTIDES= 'ATGCU'
while 1:
    dna_code=input( 'Please enter your genetic code of interest:' ).upper()
    if set(dna_code).issubset(set(NUCLEOTIDES)):
        break
    else:
        print("Input must be composed of only A/T/G/C/U")

-Mek
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1