[Tutor] Decoding

bhaaluu bhaaluu at gmail.com
Mon Aug 13 03:12:00 CEST 2007


Greetings,

>From what I can tell of this "decoding" function, it uses
the chr() function to return the ascii character:

>>> print chr(eval('65'))
A

>>> print ord('A')
65

In this textbook example, the "code" is simple a string of the
ASCII characters' numeric values. Nothing fancy.

What does this 'coded' message decode to, using the dec function?

a = "72 101 108 108 111 32 69 114 105 99 33"

Happy Programming!
-- 
bhaaluu at gmail dot com

On 8/12/07, Eric Walker <sli1que at yahoo.com> wrote:
> newbie here,
> I just tried playing around with the dec function and
> I get errors. Correct me if I am wrong. After getting
> the input, the string.split will parse the string
> across whitespace chars so in other words you get a
> list of each word entered. Then when it does the
> eval(x) part it dies. I tries to evaluate the word to
> be an existing variable or something. Am I missing
> something here.
>
> Thanks
>
> Eric
>
>
> --- Khamid Nurdiev <khamid.nurdiev at gmail.com> wrote:
>
> > Thanks, it really works.
> >
> >
> > On 8/13/07, bhaaluu <bhaaluu at gmail.com> wrote:
> > >
> > > Greetings,
> > >
> > > Disclaimer: I'm a Python Noob,
> > > so use the code snippets
> > > in this post, at your own risk!
> > >
> > > Is this what you're looking for?
> > >
> > > def dec(a):
> > >     import string
> > >     result=''
> > >     for x in string.split(a):
> > >         result=result+chr(eval(x))
> > >     return result
> > >
> > > print dec(raw_input("Enter the message to decode:
> > "))
> > >
> > > I took raw_input() out of the dec() function and
> > pass the string to the
> > > function
> > > as an argument. Or, to assign it to a variable,
> > then pass it:
> > >
> > > a=raw_input("Enter the message to decode: ")
> > > print dec(a)
> > >
> > > Or, if you want to read the code from a file on
> > disk called code.txt
> > > which has the following code in it:
> > > 65 66 67 68
> > >
> > > fin=open('code.txt','r)
> > > a = fin.read()
> > > fin.close()
> > > print dec(a)
> > >
> > > ABCD
> > > --
> > > bhaaluu at gmail dot com
> > >
> > > On 8/12/07, Khamid Nurdiev
> > <khamid.nurdiev at gmail.com> wrote:
> > > > Hello All,
> > > >  I am currently learning python with the book
> > "Python programming: An
> > > introduction to CS" by John M. Zelle and have come
> > the section where he
> > > speaks of encoding messages. Currently the basic
> > snippet looks like this:
> > > >
> > > >
> > > > >
> > > > > > def dec():
> > > > > >      import string
> > > > > >     message=raw_input("Enter the message to
> > decode: ")
> > > > > >     result=''
> > > > > >      for x in string.split(message):
> > > > > >         result=result+chr(eval(x))
> > > > > >      return result
> > > > > >
> > > > > >  print dec()
> > > > >
> > > >
> > > >
> > > > it works fine as expected but I want to enter
> > the message as a variable
> > > like:
> > > > a='12 34 84 39 122'
> > > > and when the function dec() invoked, i would
> > just enter "a" as an input
> > > and thus have changed the code a little bit but it
> > is not working.
> > > >
> > > >
> > > > >
> > > > > >  def dec():
> > > > > >     import string
> > > > > >      message=raw_input("Enter the message to
> > decode: ")
> > > > > >     a=message[1:-1]
> > > > > >     result=''
> > > > > >      for x in string.split(a):
> > > > > >         result=result+chr(eval(x))
> > > > > >      return result
> > > > > >
> > > > > > print dec()
> > > > > >
> > > > >
> > > >
> > > > Have tried many ways, i don't want to write them
> > all here as they will
> > > take too much space. None of them work. maybe you
> > guys know some way out? or
> > > where is it going wrong?
> > > >
> > > >  Thanks
> > > >
> > > > _______________________________________________
> > > > Tutor maillist  -  Tutor at python.org
> > > > http://mail.python.org/mailman/listinfo/tutor
> > > >
> > > >
> > >
> > > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
>
>       ____________________________________________________________________________________
> Fussy? Opinionated? Impossible to please? Perfect.  Join Yahoo!'s user panel and lay it on us. http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7
>
>


More information about the Tutor mailing list