Python Forum
Change each character of list into an different characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change each character of list into an different characters
#1
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
Reply
#2
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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing a few characters of a specified character but not all of them from a string fatherted99 7 3,233 Aug-13-2020, 09:08 AM
Last Post: fatherted99
  Unexpected change to a list in a small amount of self-contained code Johno 5 2,872 Mar-15-2020, 05:06 PM
Last Post: jefsummers
  Search character from 2d list to 2d list AHK2019 3 2,508 Sep-25-2019, 08:14 PM
Last Post: ichabod801
  Print strings enclosed with a character with different characters aylmaoxd 1 1,929 Aug-17-2019, 03:35 PM
Last Post: Resistance
  Remove special character from list vestkok 3 4,235 Nov-04-2018, 01:48 PM
Last Post: ichabod801
  Unexpected character after line continuation character joshyb123 5 10,652 Sep-05-2018, 08:08 AM
Last Post: perfringo
  unexpected character after line continuation character error newbie 10 14,669 Aug-09-2018, 06:07 PM
Last Post: nilamo
  Sorting list of names by first two characters Otbredbaron 2 3,283 May-24-2018, 03:59 PM
Last Post: Otbredbaron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020