How can spaces be ignored in the input, but printed at the end?
Solution 1:
To solve your problem with minimal changes, you can just add a special check for the space:
...
for i, ch in enumerate(encrypted):
if encrypted[i-1] == ' ':
decoded += ' '
elif i == 0 or ch == encrypted[i - 1]:
...
But you can opt for a simpler way by using groupby
:
from itertools import groupby
def main():
encrypted = "** *"
# input("Enter an encrypted message: ")
decoded = ""
for key, group in groupby(encrypted):
if key == ' ':
decoded += ' '
else:
decoded += chr(sum(1 for _ in group) + 64)
print(decoded)
main()