ajknzhol
ajknzhol

Reputation: 6450

Iterating over list

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

Answers (6)

Ray
Ray

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

charego
charego

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

Guy Gavriely
Guy Gavriely

Reputation: 11396

def encrypt(string):
  return ''.join([encrypt_letter(letter) for letter in string])

output:

uryyb

Upvotes: 0

GMPrazzoli
GMPrazzoli

Reputation: 239

The problem is that

    print "".join(result)

falls in the for loop.

Upvotes: 1

user2555451
user2555451

Reputation:

You are printing out the list with each iteration of the for-loop. You need to do two things to fix the problem:

  1. Dedent print "".join(result) one level.

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

jonrsharpe
jonrsharpe

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 returned from your function, add:

return result

at the end (either additional to or replacing the print) to send your output to e.

Upvotes: 1

Related Questions