Quite the beginner here. So basically, this is an exercise to transform an input string of letters into its unicodes and back into a string (I know it's silly, but it's an exercise eheh). So my code is:
OG_string = ""
while OG_string.isalpha() is False:
OG_string = input("Enter a message: ")
if not OG_string.isalpha():
print("Sorry, you can only enter letters")
secret_string = ""
for char in OG_string:
secret_string += str(ord(char))
print("The secret code is: ", secret_string)
OG_string = ""
for i in range(0, len(secret_string)-2, 2):
if int(secret_string[i]) == 1:
unicode = secret_string[i] + secret_string[i + 1] + secret_string[i + 2]
i += 1
else:
unicode = secret_string[i] + secret_string[i + 1]
OG_string += chr(int(unicode))
print("The original message is: ",OG_string)
It was working easy with just Uppercase cause all the numbers were 2 digits (with len(secret_string)-1 instead of 2), but now that some numbers are 3 digits (most lowercase's unicodes), I can't make it work and the original message I get out is weird because it seems to work for the first 3 letters and not the rest(if I input "Hello"). Here's an example:
Enter a message: Hello
The secret code is: 72101108108111
The original message is: HelQ
If I enter "Banana" I get (The original message is: Ban Gm). I get that Q's unicode is 81 which appears later on but why does it do that? One solution was to subtract 23 while converting to ord and add it back when you do the chr on the 2nd to last line (since z is 122), but I wanted to try it through another route.
Thanks in advance for any assistance ;)
P.S. For my original Do-while loop, is there any reason to prefer a "While True" followed by a "break" somewhere in it over doing what I did? Seems like the teacher does "While True" but IDK if there's a difference. Also I did both a "while X...False" and "if not X" to remind myself that both worked, but are they always interchangeable?
