1

I am learning python right now, and as an exercise, I am trying to create a program that encodes or decodes a string according to a caesar shift cipher where the shift of the cipher can be input by a user. However, when I run it, i get the error:

Traceback (most recent call last):
  File "exercises.py", line 52, in <module>
    print e(input)
  File "exercises.py", line 10, in e
    slist = s.split()
AttributeError: 'builtin_function_or_method' object has no attribute 'split'.

Can Anyone help me with this? Here is the code:

import time
import string

print "Welcome to the Caesar Shift Cipher Encoder/Decoder"
time.sleep(2)
ed = raw_input("Do you want to encode or decode (e/d)? \n")
alphabet = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')

def e(s):
    slist = s.split()

    key = {'a':'', 'b':'', 'c':'', 'd':'', 'e':'', 'f':'', 'g':'', 'h':'', 
    'i':'', 'j':'', 'k':'', 'l':'', 'm':'', 'n':'', 'o':'', 'p':'', 
    'q':'', 'r':'', 's':'', 't':'', 'u':'', 'v':'', 'w':'', 'x':'',
    'y':'', 'z':''}

    input1 = raw_input("Type what you want to encode \n")
    shift = int(raw_input("What is the shift of the cipher \n"))
    for x in key:
        if (alphabet.index(x) + shift) > 25:
            key[x] = alphabet[((alphabet.index(x)) + shift) - 26]
        else:
            key[x] = alphabet[((alphabet.index(x)) + shift)]

    for letter in slist:
        letter = key[letter]
    result = " ".join(slist)    
    return result

def d(s):
    slist = s.split()

    key = {'a':'', 'b':'', 'c':'', 'd':'', 'e':'', 'f':'', 'g':'', 'h':'', 
    'i':'', 'j':'', 'k':'', 'l':'', 'm':'', 'n':'', 'o':'', 'p':'', 
    'q':'', 'r':'', 's':'', 't':'', 'u':'', 'v':'', 'w':'', 'x':'',
    'y':'', 'z':''}

    input1 = raw_input("Type what you want to decode \n")
    shift = int(raw_input("What is the shift of the cipher \n"))
    for x in key:
        if (alphabet.index(x) - shift) > 25:
            key[x] = alphabet[((alphabet.index(x)) - shift) - 26]
        else:
            key[x] = alphabet[((alphabet.index(x)) - shift)]

    for letter in slist:
        letter = key[letter]
    result = " ".join(slist)    
    return result

if ed == "e":
    print e(input)
elif ed == "d":
    print d(input)
else:
    print "That is not an option. Please try again."
    ed = raw_input("Do you want to encode or decode (e/d)? \n")
1
  • input is not defined in the script you've posted, so e(input) (and then splitting on input) will likely not do what you want. I would add print s, type(s) to the top of your e function, it should provide helpful information Commented Dec 13, 2015 at 23:11

3 Answers 3

1

You call your functions e or d with input, which is a built-in (and no string). Ask for a string before that and hand that string over to your functions.

Sign up to request clarification or add additional context in comments.

Comments

1

The reason that you are getting an error is because you are passing input to your function. input is a built-in function in python. Since you did not re-define input the actual function is being passed to your functions e and d. You can't split a built-in function. You need too pass the functions a string.

It looks like you don't even need to pass anything to your functions though. Try moving the split to after your input1 line and splitting input1 instead of s. This would make more sense then what you are currently trying to do.

Another thing to look at is in your function d. alphabet.index(x) - shift will never be greater than 25 but it could be less than 0. You probably will need to change this.

1 Comment

thanks! your advice about my function d was helpful.
-1

You need a string to apply .split

Cast the input to a string like such:

if ed == "e":
    print e(str(input))
elif ed == "d":
    print d(str(input))

2 Comments

also I changed the name of the variable input so it was not a built in
and defined the variable outside the functions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.