Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

NameError: name 'maketrans' is not defined, pythonchallenge

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


circularfunc at yahoo

Jun 24, 2008, 10:55 AM

Post #1 of 4 (102 views)
Permalink
NameError: name 'maketrans' is not defined, pythonchallenge

im doing the python challenge and well, i solved this problem with
ruby :)
phrase = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq
ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr
gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc
spj."
puts phrase .tr('A-XY-Za-xy-z','C-ZA-Bc-za-b')

the answer says to use maketrans but i cant get it to work:

>>> pat = maketrans('A-XY-Za-xy-z', 'C-ZA-Bc-za-b')

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
pat = maketrans('A-XY-Za-xy-z', 'C-ZA-Bc-za-b')
NameError: name 'maketrans' is not defined

>>> "hej tjena tjenixen".maketrans('A-XY-Za-xy-z', 'C-ZA-Bc-za-b')

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
"hej ditt fetto".maketrans('A-XY-Za-xy-z', 'C-ZA-Bc-za-b')
AttributeError: 'str' object has no attribute 'maketrans'

http://docs.python.org/lib/node41.html
--
http://mail.python.org/mailman/listinfo/python-list


circularfunc at yahoo

Jun 24, 2008, 11:11 AM

Post #2 of 4 (99 views)
Permalink
Re: NameError: name 'maketrans' is not defined, pythonchallenge [In reply to]

from string import maketrans

ok but how can i write:
pattern = maketrans('A-XY-Za-xy-z', 'C-ZA-Bc-za-b')
pattern = maketrans('A-Za-z', 'C-Bc-b')
none works....
--
http://mail.python.org/mailman/listinfo/python-list


circularfunc at yahoo

Jun 24, 2008, 11:30 AM

Post #3 of 4 (97 views)
Permalink
Re: NameError: name 'maketrans' is not defined, pythonchallenge [In reply to]

import string
text = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq
ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr
gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc
spj."
table = string.maketrans(string.ascii_lowercase,
string.ascii_lowercase[2:]+string.ascii_lowercase[:2])
print string.translate(text,table)


--
http://mail.python.org/mailman/listinfo/python-list


ggpolo at gmail

Jun 24, 2008, 11:50 AM

Post #4 of 4 (98 views)
Permalink
Re: NameError: name 'maketrans' is not defined, pythonchallenge [In reply to]

On Tue, Jun 24, 2008 at 3:11 PM, cirfu <circularfunc[at]yahoo.se> wrote:
> from string import maketrans
>
> ok but how can i write:
> pattern = maketrans('A-XY-Za-xy-z', 'C-ZA-Bc-za-b')
> pattern = maketrans('A-Za-z', 'C-Bc-b')
> none works....

maketrans doesn't work like that, you would need something like this:

sfrom = string.uppercase + string.lowercase
sto = string.uppercase[2:] + string.uppercase[:2] +
string.lowercase[2:] + string.lowercase[:2]
table = string.maketrans(sfrom, sto)

print string.translate(phrase, table)

> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
-- Guilherme H. Polo Goncalves
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.