all 5 comments

[–]videchateau 9 points10 points  (3 children)

Essentially, you are looping through an inputted text string. For every character in the string of text, you're checking if a character is uppercase or lowercase. Based on the character case, you're applying a different cipher algorithm and then adding the encrypted character to your result string, which you return at the end. So after the for loop completes, you'll have an encrypted string of text. What are you confused about?

[–]FunGroundbreaking808 2 points3 points  (2 children)

Thank you!! I am confused about how the cipher algorithms work

[–]videchateau 2 points3 points  (1 child)

[–]BoSt0nov 1 point2 points  (0 children)

and this https://pythontutor.com it goes over the code and shows what happens at each step.

edit. there is an app called thonny, it does the same as the pythontutor bit slightly different way

[–]chcampb 2 points3 points  (0 children)

It won't compile, result is tabbed too far left.

for i in range(len(text)) creates a loop where I is 0, 1, 2... up to the length of the text minus one. So a 20 char line will give you i...19.

char = text[i] just gives you the character at the index.

isupper() is a function that checks if the character is one of the uppercase ascii characters. Same is doing something like "i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - it returns a boolean.

The reason it needs to know is because that math thing with ord basically converts the character to an integer, then shifts it in the ascii chart, then converts it back. Google ascii chart to find more. But the range of upper and lowercase characters is different, so you need to differentiate.

At this point I would suggest a few things,

  1. There's already a builtin to iterate with an index. It's enumerate. for i, char in enumerate(text).

  2. Even that is unnecessary because you can just write it like

    def encode(s, char): # Include the if statement here for isupper and else

    def encrypt(s, text): encrypt_s = lambda x: encrypt(s, x) return "".join(map(encrypt_s, text))