0
Problem with exercise
The exercise asks me to replace each letter with it corresponding letter in the inverted alphabet (if there is an "a", need to replace it with a "z"). Here is my code. I am a bit newbie in Thai and this is the first "medium" exercise that I do. I receive an error in the line 5, anyone can help me? The code: str = input() alphabet = "abcdefghijklmnopqrstuvwxyz" re_alph = (alphabet[::-1]) for i in range(len(str)): str[i] = letter alphabet [letter] = place re_alph [place] = str[i] print (str)
7 Answers
+ 4
The problems appear to be in your for loop. When you say str[i] = letter, I think you mean to put letter = str[i], that means the variable letter is being assigned the value of the character at index i of str. (As a side note, it is not a good idea to use 'str' as a variable name as str() is used as a method to change a value to a string.)
The same thing for the next line; alphabet[letter] = place, it should be the other way around, but here, letter is not an integer so you can't call the character at index 'letter' of the string alphabet, you need alphabet.index(letter) - this returns the index of 'alphabet' that 'letter' appears. I think your reversal habit is present in the next lone too.
Lastly, you cannot amend individual characters of a string. str[i] = re_alph[place] is just not something you can do. I'd advise you to create a new empty string at the start (call it 'output' or something), and with each character you decode, add it to your output string and print that at the end.
+ 2
letter hasn't been defined yet, and I believe the error is on line 6(str[i] = letter). I believe you mean:
letter = str[i]
There's another error in line 7, which I believe is typed backwards, again. You can correct those two errors, and see what else is wrong.
+ 1
Easiest way is output += re_alph[place]
0
Anda how can I do it? I am forgetting something because it only shows the last letter if I write this:
Output = re_alph[place]
0
Ok, i figured out (another way, more complicated so thank you)
Now I have a problem, I guess, with the space character because I receive a "substring not found" in this code when I try to index the space. I added the space to the string in the "alphabet" variable but it doesn't recognize it
words = input()
alphabet = "abcdefghijklmnopqrstuvwxyz"
re_alph = "zyxwvutsrqponmlkjihhfedcba"
output = ""
out = ""
for i in range(len(words)):
letter = words [i]
place = int(alphabet.index(letter))
output = re_alph [place]
out += output
print (out)
0
Hello Alexander, the input is upper, lower and spaces I guess because Sololearn doesn't show me the last 3 tests if I am not Pro
0
As I said, the task is to take a string (with upper, lower and spaces at least) and replace every character with its counterpart in an inverted alphabet. I can do it with single words (receiven only lower output) but the code fails when I have a string with spaces in it, and I don't know how to ignore it because the expected output still has the spaces, I can't eliminate them