2

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?

2
  • 1
    Don't store the Ords in a string, but in a list, so you'll have clear separations. Commented Oct 15, 2020 at 1:35
  • Oh thanks, and then I'd "print" with a For loop I guess? Commented Oct 15, 2020 at 21:26

3 Answers 3

3

Don't monkey with the for loop's control variable inside the loop.

That is, rewrite:

for i in range(10):
    if i % 3 == 0:
        i += 1
    print(i)

...to:

i = 0
while i < 10:
    if i % 3 == 0:
        i += 1
    print(i)
    i += 1
Sign up to request clarification or add additional context in comments.

1 Comment

Ok thanks got it, Idk why the teacher showed it that way, he also showed an example that couldof worked with int (and maybe a conditional) with "eval" and I heard it was a security risk lol.
2

Your for loop iterates through a fixed group of objects, so your i+=1 line there had no effect. To change this, I suggest using a while loop:

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 = ""

i=0
while i<len(secret_string)-2:
    if int(secret_string[i]) == 1:
       unicode = secret_string[i:i+3]
       i += 3
    else:
       unicode = secret_string[i:i+2]
       i += 2
    print(unicode)
    OG_string += chr(int(unicode))

print("The original message is: ",OG_string)

4 Comments

Oh great that's smart, I figured there'd be a more effective way then just adding each individual "i", forgot about the ":", thanks. So would the for loop ever be more useful for this kind of thing? IDK why the teacher goes for that way (guess to teach new material), your while loop idea is way better. I know you've already answered my main question, but anything about While True vs While "condition" with an empty variable + break? What about False vs "not"? It doesn't seem like you changed that part of my code, so I guess it makes no difference? Anyway Thanks for the help !
more effective way than*. Also I should have said that the "break" is actually paired with the "while True", my bad.
btw, there was a mistake I discovered if the last character was a capital, so I changed it to: while i < len(secret_string) - 1: if int(secret_string[i]) == 1 and i < len(secret_string) - 2:
actually just i < len(secret_string) - 1 ...was necessary, my bad
1
OG = ""
while OG.isalpha() is False:
    OG = input("Enter a message: ")
    if not OG.isalpha():
        print("Sorry, you can only enter letters")

asc = []

for char in OG:
    asc.append(str(ord(char)))

print("The secret code is: ", asc)
print( ''.join(asc )  )   # if you want a string 

OG2=""

for i in range(len(asc)):
    OG2 += chr(int(asc[i]))

print("The original message is: ",OG2)

enter image description here

4 Comments

Oh sweet cool idea! So if I get this right, it'd create a list and then each "i" would be an item (each unicode ) in the list? That solution is so much shorter and elegant, thanks.
In python the for loop automatically iterates through a list : ` for item in list` without need for an index i. That is similar to Java/C# "foreach" and different from C.
So isn't it simpler to do...... for i in asc:........ OG2 += chr(int(i))
Yes, that is the right syntax. i here is a string, not a number, because the list is a list of strings. I don't use i for such variables, because it implies (at least to us old-timers) i being the index of a C-style for loop. See e.g. en.wikipedia.org/wiki/For_loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.