+ 1

I came up with this code and its throwing back an error when the input is uppercase...Who can help me take care of that

alphabet="abcdefghijklmnopqrstuvwxyz" re_alpha=alphabet[::-1] sub=dict(zip(alphabet,re_alpha )) msg=str(input()) r_msg="".join(sub[x] for x in msg.replace("",'')) print(r_msg)

28th Dec 2021, 10:22 AM
George Williams
George Williams - avatar
5 Réponses
+ 6
George Williams , just 2 remarks: (1) the replace() method is replacing an empty string with an empty string: r_msg="".join(sub[x] for x in msg.replace("",'')) if you wanted to remove spaces in the input string, you should correct the code. otherwise an error occurs when the input contains spaces (2) if you wanted to keep the spaces you could use this variation of your code: r_msg = "".join(sub.get(x, " ") for x in msg)
28th Dec 2021, 8:15 PM
Lothar
Lothar - avatar
+ 3
msg = input().lower() # input is a string by default
28th Dec 2021, 10:45 AM
Slick
Slick - avatar
+ 2
you could add a key: value pair of {' ': ' '} to the dictionary that's called sub. Add it after you make the dictionary https://code.sololearn.com/cbDCj9QXdoB0/?ref=app
28th Dec 2021, 12:05 PM
Slick
Slick - avatar
+ 1
Thanks but it is still throwing back an error because the code doesn't take care of space for instance when the input is:You are Amazing How do i make the code to take care of the spacing i have no idea
28th Dec 2021, 11:43 AM
George Williams
George Williams - avatar
+ 1
Thanks so much meeehhnn..I appreciate
28th Dec 2021, 12:17 PM
George Williams
George Williams - avatar