Python Forum

Full Version: Change each character of list into an different characters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

Please could someone help with the error I am getting with my code

Description
Change each character of an list into different characters. Below is the sample example:

u became y,
p became t,
G became K,
r became v,
a became e,
d became h,

Output:
Input ['banana', 7] Solution output ihuhuh Expected output utgtgt Input ['ytKveh', 4] Solution output cxOzil Expected output upGrad
Sorry posted wrong code and error!!!!also just to let you know what need to be altered in the code to get the desired output.

#take input on your own
#start writing your code from here
import ast
mylist=ast.literal_eval(input())
stringMessage=mylist[0]
shift_value=mylist[1]

def swap_letters(word, step_count):
    final_word = ""
    for x in list(word):
#         for lowercase letters crossing the Ascii values
        if(ord(x)+step_count>122 and ord(x)>97):
            w = ((ord(x)+step_count)-122)+96
            y = chr(w)
            final_word = final_word + y
            
#           for lowercase letters crossing the Ascii values
        elif(ord(x)+step_count>91 and ord(x)<91):
            w = ((ord(x)+step_count)-90)+64
            y = chr(w)
            final_word = final_word + y
            
#           for normal letters within the Std Ascii Values
        else:
            y = chr(ord(x)+step_count)
            final_word = final_word + y
    return final_word
    
print(swap_letters(stringMessage,shift_value))
OK, I'm really new to python can anyone help me to correct the code
Got Results:

Output:
import ast mylist=ast.literal_eval(input()) stringMessage=mylist[0] shift_value=mylist[1] #start writing your code from here def shift_string(message, n): list1=[] for i in message: ch = i base = ord('a' if ch.islower() else 'A') x = chr((ord(ch) - base - n) % 26 + base) list1.append(x) return("".join(list1)) print(shift_string(stringMessage,shift_value))