[Tutor] Decoding

bhaaluu bhaaluu at gmail.com
Sun Aug 12 23:01:25 CEST 2007


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
>
>


More information about the Tutor mailing list