By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,706 Members | 1,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,706 IT Pros & Developers. It's quick & easy.

NameError: name 'maketrans' is not defined, pythonchallenge

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies

P: n/a
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....
Jun 27 '08 #2

P: n/a
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)
Jun 27 '08 #3

P: n/a
On Tue, Jun 24, 2008 at 3:11 PM, cirfu <ci**********@yahoo.sewrote:
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
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.