Reputation: 13
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")
Upvotes: 1
Views: 1521
Reputation: 1224
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.
Upvotes: 1
Reputation: 10086
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.
Upvotes: 1
Reputation: 1607
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))
Upvotes: -1