+ 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)
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)
+ 3
msg = input().lower() # input is a string by default
+ 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
+ 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
+ 1
Thanks so much meeehhnn..I appreciate