NameError: name 'maketrans' is not defined, pythonchallenge

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cirfu

    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.kyicrpyl q() 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".maket rans('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".maketran s('A-XY-Za-xy-z', 'C-ZA-Bc-za-b')
    AttributeError: 'str' object has no attribute 'maketrans'


  • cirfu

    #2
    Re: NameError: name 'maketrans' is not defined, pythonchallenge

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

    Comment

    • cirfu

      #3
      Re: NameError: name 'maketrans' is not defined, pythonchallenge

      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.kyicrpyl q() gq pcamkkclbcb. lmu ynnjw ml rfc
      spj."
      table = string.maketran s(string.ascii_ lowercase,
      string.ascii_lo wercase[2:]+string.ascii_l owercase[:2])
      print string.translat e(text,table)


      Comment

      • Guilherme Polo

        #4
        Re: NameError: name 'maketrans' is not defined, pythonchallenge

        On Tue, Jun 24, 2008 at 3:11 PM, cirfu <circularfunc@y ahoo.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.uppercas e + string.lowercas e
        sto = string.uppercas e[2:] + string.uppercas e[:2] +
        string.lowercas e[2:] + string.lowercas e[:2]
        table = string.maketran s(sfrom, sto)

        print string.translat e(phrase, table)


        --
        -- Guilherme H. Polo Goncalves

        Comment

        Working...