| | |
Scanning a string?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
AWSOME!!! Thank you soo much, ok wow, lol I basically, (i think) was on the same track as you, but I kept getting a syntax error, heres the code n/e ways.
Python Syntax (Toggle Plain Text)
decoderdict = {"aaa": "a", "bbb": "b", "ccc": "c", "tcg": "d"} while True: dna = raw_input("Please enter the DNA string: ").lower() if dna == "exit": break output = "" for triplet in range(0,1,3): decoderdict.get(dna[triple], "error") += finals if finals == "error": break if finals != "error": print "The decoded string is: ", finals.upper() else: print "One of the triples in your code could not be decoded."
Blender is by far the best free 3D computer Graphics program and it is expandable with python! GoTo www.blender3d.org
Several syntax problems:
- You have "for triplet in range(0,1,3):" but later reference "triple", which is not a variable
- You create output as an empty string, and then never reference it. Either change that to finals or all of the instances of "finals" to "output".
Now that the program works... there are some semantic errors:
- Do you know what "range(0,1,3)" does? It starts with 0 and goes by threes from 0 to 1. So basically it is [0] (list). You want to approach this problem a little different. You don't want to do it by indexes because you are getting a string of THREE letters. I guess you could figure out how to do it by slicing, but I didn't choose to approach it that way.
Note:
The range function works like this:
Range(max) generates the list [0, 1, ... , (max-1)].
Range(min,max) generates the list [min, (min+1), ... , (max-1)].
Range(min,max,step) does the same as the previous except goes by the increment step. step cannot be 0. If step is negative, then the role of min and max are reversed.
- You have "for triplet in range(0,1,3):" but later reference "triple", which is not a variable
- You create output as an empty string, and then never reference it. Either change that to finals or all of the instances of "finals" to "output".
Now that the program works... there are some semantic errors:
- Do you know what "range(0,1,3)" does? It starts with 0 and goes by threes from 0 to 1. So basically it is [0] (list). You want to approach this problem a little different. You don't want to do it by indexes because you are getting a string of THREE letters. I guess you could figure out how to do it by slicing, but I didn't choose to approach it that way.
Note:
The range function works like this:
Range(max) generates the list [0, 1, ... , (max-1)].
Range(min,max) generates the list [min, (min+1), ... , (max-1)].
Range(min,max,step) does the same as the previous except goes by the increment step. step cannot be 0. If step is negative, then the role of min and max are reversed.
ok gotcha, lol i read the range thing wrong, I thought that i was saying go up 3 in increments of 1 from 0 lol oops
EDIT: Well, thank you for the help, Ill let you know when the decoder is fininshed, I'm also making a coder too, I have the beta versions out, but they are very cumbersome, so yeah, ttyl and thank you soo much for you help!
EDIT: Well, thank you for the help, Ill let you know when the decoder is fininshed, I'm also making a coder too, I have the beta versions out, but they are very cumbersome, so yeah, ttyl and thank you soo much for you help!
Last edited by danizzil14; Feb 11th, 2007 at 1:56 am.
Blender is by far the best free 3D computer Graphics program and it is expandable with python! GoTo www.blender3d.org
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
Another way:
Regards, mawe
Python Syntax (Toggle Plain Text)
def group( lst, n ): ''' splits an iterable in parts with length n, e.g. group( [1,2,3,4], 2 ) ==> [ (1,2), (3,4) ] ''' return zip( *[ lst[i::n] for i in range(n) ] ) aa_dict = { "AAA": "A", "GGG": "C", "CCC": "B" } s = "AAAGGGCCC" g = [ "".join(t) for t in group(s, 3) ] for triplet in g: print aa_dict[triplet],
Regards, mawe
•
•
Join Date: Oct 2008
Posts: 6
Reputation:
Solved Threads: 0
simplified version of an Encryptor/Decryptor i did a while ago
the parts with the pointy
Hope that helps,
Regard,
{Anonamemoose}
the parts with the pointy
python Syntax (Toggle Plain Text)
l='' # a place to attach the results of <i in dictionary> to# for i in <NameOfInput/TheThingYouWantToEncrypt>: if i in <NameOfdictionary>: L=L+<NameOfdictionary>[<NameOfInputInsideBrackets>] #attach the value of the dictionary to each letter to the empty string# # can be written as L+= instead of L=L+, and is another way to join/append to a list/tuple else: pass # you can also insert<print i> if u want it to print a char unknown to the dictionary!# #the pass just tells him to ingore that letter# print l #print the final output, if i would put it under the if/else idention, it would have print each step of the encryption on a different line#
Regard,
{Anonamemoose}
Last edited by A no-name-moose; Oct 4th, 2008 at 10:57 pm.
A no-name-moose, rather than writing template looking code with
The advantage is that your code is immediately usable by others !
<NameOfDictionary> things, whihc are not in the syntax of python, you should better write a python function with local variables, like this python Syntax (Toggle Plain Text)
def crypt(input_sequence, dictionary): L = '' for i in input_sequence: if i in dictionary: L += dictionary[i] return L
![]() |
Similar Threads
Other Threads in the Python Forum
- Previous Thread: A Little WordGame problem :)
- Next Thread: GUI Question
Views: 3085 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for Python
approximation array beginner book builtin change cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java library line lines linux list lists loop microcontroller mouse mysqldb mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect remote script scrolledtext search singleton socket sqlite ssh string strings strip subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable vigenere wikipedia windows wxpython xlwt





