0

I'm trying to complete ROT13 codewars challenge and that's my last issue I got. I keep receiving

['B']
['e']
['S']
['t']

Instead of

['B','e','S','t']

Entire code if you are wondering

def rot13(message):
    for x in message:
        alphabet = 'abcdefghijklmnopqrstuvwxyz'
        pre = alphabet[:13]
        after = alphabet[13:]
        result=[]
        
        if x != x.lower():
            alphabet = alphabet.upper()
            pre = pre.upper()
            after = after.upper()
            if x in pre:
                a=pre.find(x)
                b=pre[a]
                result.append(b)
            elif x in after:
                a = after.find(x)
                b = after[a]
                result.append(b)
        
        elif x == x.lower():
            if x in pre:
                a=pre.find(x)
                b=pre[a]
                result.append(b)
            elif x in after:
                a = after.find(x)
                b = after[a]
                result.append(b)

        print(result)

rot13('BeSt')
JNevill
  • 46,980
  • 4
  • 38
  • 63
kacperj513
  • 31
  • 5

1 Answers1

1

Based on the code sample provided, fixing this requires fairly straightforward changes. Move your result=[] outside of your for loop (so that it doesn't get emptied after each iteration), and then print the result also outside the loop so that you don't print one part of the loop 4 times.

The complete code is below, but note that the code provided does not currently implement Rot13 correctly. There are many other Stack Overflow questions for how to do this, e.g.: How to ROT13 encode in Python3?

def rot13(message):
    result=[]
    for x in message:
        alphabet = 'abcdefghijklmnopqrstuvwxyz'
        pre = alphabet[:13]
        after = alphabet[13:]
        
 
        if x != x.lower():
            alphabet = alphabet.upper()
            pre = pre.upper()
            after = after.upper()
            if x in pre:
                a=pre.find(x)
                b=pre[a]
                result.append(b)
            elif x in after:
                a = after.find(x)
                b = after[a]
                result.append(b)
 
        elif x == x.lower():
            if x in pre:
                a=pre.find(x)
                b=pre[a]
                result.append(b)
            elif x in after:
                a = after.find(x)
                b = after[a]
                result.append(b) 
    print(result)

The output from this with input rot13('BeSt') is ['B', 'e', 'S', 't'].