You're getting the extra "w"s because of an interaction between the output of str.find() and str[index]. The "original" string does not include a space as a character, therefore, str.find() returns a -1 per the documentation. If you index an iterable with a -1, the interpreter returns the last item in the iterable:
To correct this, you'll either have to control for a -1 result from line 19 or rewrite the loop to avoid the issue. You could split "text" by spaces and then join it again later after encrypting.
x = "abc" print(x[-1]) # Prints "c"So, every time your loop encounters a space...
- it searches original for the character
- fails to find it and returns a -1
- indexes shifted with a -1 to return the last character ("w" in this case)
- appends that character in place of the space
To correct this, you'll either have to control for a -1 result from line 19 or rewrite the loop to avoid the issue. You could split "text" by spaces and then join it again later after encrypting.