Bug in reverse alphabet code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Bug in reverse alphabet code

The code works in general except when a letter replaced by a letter and then followed by the same letter. Then it "skips" that letter. I don't understand where the problem is. For example input "piz" outputs "kra" which is correct. But input "pizza" outputs "krzzz" which is wrong. dict = {"a": "z", "b": "y", "c": "x", "d": "w", "e": "v", "f": "u", "g": "t", "h": "s", "i": "r", "j": "q", "k": "p", "l": "o", "m": "n", "n": "m", "o": "l", "p": "k", "q": "j", "r": "i", "s": "h", "t": "g", "u": "f", "v": "e", "w": "d", "x": "c", "y": "b", "z": "a"} x = input() for i in x: x = x.replace(i, dict.get(i)) print(x),

9th Apr 2022, 11:26 AM
Walter Zwiekhorst
Walter Zwiekhorst - avatar
4 Answers
+ 2
I came up with this alternative solution :) x = input().lower() result = ""   alph = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] revalph = alph[::-1]   for i in x:     if i in alph:         ind = alph.index(i)         result += revalph[ind]     else:         result += i         print(result)
9th Apr 2022, 11:55 AM
Walter Zwiekhorst
Walter Zwiekhorst - avatar
+ 2
alph = list(map(chr, range(ord('a'), ord('z')+1))) # Alphabet a to z
9th Apr 2022, 1:02 PM
SoloProg
SoloProg - avatar
+ 1
Wow so you typed all the keys and values 😊 Thats a lot of work, find a work around Back to the question Okay so the thing is replace replaces all the duplicates So what you need to do is, pass the second parameter of replace so In general the syntax of replace is .replace(new, old, count) So you can specify a count which is, 1 in your case to affect just one character
9th Apr 2022, 11:42 AM
Faisal Issaka
Faisal Issaka - avatar
+ 1
Okay thats a bit okay
9th Apr 2022, 12:02 PM
Faisal Issaka
Faisal Issaka - avatar