Reputation: 6450
I am currently doing a small pet project and i have come this far, currently my code accepts the string, changes the string into the respective cipher and displays it but its displaying the whole iterated string. What i am doing wrong ? i want only the translated string.
Code
def encrypt_letter(letter):
cipher = {'a': 'n', 'b': 'o', 'c': 'p', 'd': 'q',
'e': 'r', 'f': 's', 'g': 't', 'h': 'u',
'i': 'v', 'j': 'w', 'k': 'x', 'l': 'y',
'm': 'z', 'n': 'a', 'o': 'b', 'p': 'c',
'q': 'd', 'r': 'e', 's': 'f', 't': 'g',
'u': 'h', 'v': 'i', 'w': 'j', 'x': 'k',
'y': 'l', 'z': 'm'}
lowercase_letter = letter.lower()
return cipher[lowercase_letter]
def encrypt(string):
result = []
letters = list(string)
for letter in letters:
encrypted_letter = encrypt_letter(letter)
result.append(encrypted_letter)
print "".join(result)
e = encrypt("hello")
print e
Output
u
ur
ury
uryy
uryyb
None
Expected output
'uryyb'
Upvotes: 0
Views: 88
Reputation: 2508
One minor change will do! What you want is to return
the string.
def encrypt(string):
result = []
letters = list(string)
for letter in letters:
encrypted_letter = encrypt_letter(letter)
result.append(encrypted_letter)
return "".join(result) # change to return
e = encrypt("hello")
print e # will give you expected output
In fact, you can write shorter code with this:
def encrypt(string):
cipher = {'a': 'n', 'b': 'o', 'c': 'p', 'd': 'q',
'e': 'r', 'f': 's', 'g': 't', 'h': 'u',
'i': 'v', 'j': 'w', 'k': 'x', 'l': 'y',
'm': 'z', 'n': 'a', 'o': 'b', 'p': 'c',
'q': 'd', 'r': 'e', 's': 'f', 't': 'g',
'u': 'h', 'v': 'i', 'w': 'j', 'x': 'k',
'y': 'l', 'z': 'm'}
return ''.join(cipher[s] for s in string.lower())
Upvotes: 3
Reputation: 137
I'd return a string from encrypt
, not print one.
def encrypt(string):
result = ""
for letter in list(string):
result += encrypt_letter(letter)
return result
print(encrypt("hello"))
Upvotes: 0
Reputation: 11396
def encrypt(string):
return ''.join([encrypt_letter(letter) for letter in string])
output:
uryyb
Upvotes: 0
Reputation: 239
The problem is that
print "".join(result)
falls in the for loop.
Upvotes: 1
Reputation:
You are printing out the list with each iteration of the for-loop. You need to do two things to fix the problem:
Dedent print "".join(result)
one level.
In that line, change print
to return
.
Here is the full code:
def encrypt_letter(letter):
cipher = {'a': 'n', 'b': 'o', 'c': 'p', 'd': 'q',
'e': 'r', 'f': 's', 'g': 't', 'h': 'u',
'i': 'v', 'j': 'w', 'k': 'x', 'l': 'y',
'm': 'z', 'n': 'a', 'o': 'b', 'p': 'c',
'q': 'd', 'r': 'e', 's': 'f', 't': 'g',
'u': 'h', 'v': 'i', 'w': 'j', 'x': 'k',
'y': 'l', 'z': 'm'}
lowercase_letter = letter.lower()
return cipher[lowercase_letter]
def encrypt(string):
result = []
letters = list(string)
for letter in letters:
encrypted_letter = encrypt_letter(letter)
result.append(encrypted_letter)
######################
return "".join(result)
######################
e = encrypt("hello")
print e
Output:
uryyb
Upvotes: 2
Reputation: 122024
Your print
is inside the for
loop; dedent it one level, i.e.
result.append(encrypted_letter)
print "".join(result)
This will prevent the repeated printing out through the loop. Also, nothing gets return
ed from your function, add:
return result
at the end (either additional to or replacing the print
) to send your output to e
.
Upvotes: 1