-1

I created a small program using the ASCII table. I am encrypting the string the user inputs. What I'm confused about is the "\" is its division? Or what is it separating?

plainText = input("Enter a one-word, lowercase message: ")
distance = int(input("Enter the distance value: "))
code = ""
for ch in plainText:
    ordValue = ord(ch)
    print(1, ordValue)
    print(2, ch)
    cipherValue = ordValue + distance
    print(3, cipherValue)
    if cipherValue > ord('z'):
#                                    what does "\" stand for 
        cipherValue = ord('a') + distance - \
                      (ord('z') - ordValue + 1)
        print(4, cipherValue)
    code += chr(cipherValue)
    print(5, code)
ds007
  • 35
  • 2
  • 9
  • I believe it escapes the newline character that immediately follows it, so that it doesn't cause issues with parsing. – StardustGogeta Jul 25 '19 at 18:28
  • 2
    it's just a line continuation so you can break the code across multiple lines. does not have any impact on the program. – Rick Jul 25 '19 at 18:28
  • See https://stackoverflow.com/questions/4172448/is-it-possible-to-break-a-long-line-to-multiple-lines-in-python/4172465 for reference – StardustGogeta Jul 25 '19 at 18:28
  • It's a line continuation character, that allows one long line of code to be broken into multiple lines. – Ken White Jul 25 '19 at 18:28
  • interesting that makes sense I got help from someone else but never it made it clear. thank you @RickTeachey – ds007 Jul 25 '19 at 18:31
  • 1
    thank you for the link @StardustGogeta – ds007 Jul 25 '19 at 18:31

2 Answers2

1

\ escapes the newline. That means that if the line is an expression, it tells the parser to use the next line as part of the same line.

So

x + 2 + \
  3

is the same as x + 2 + 3.

This is especially helpful with a bunch of repeated method calls, like:

my_object \
  .method_a() \
  .method_b(c) \
  .method_d(e=f) \
  .method_g()

which can sometimes be more readable than my_object.method_a().method_b(c).method_d(e=f).method_g()

More about this behavior can be found in the Python language reference.

Edward Minnix
  • 2,889
  • 1
  • 13
  • 26
0

Forward slash is division [ / ]. You're using backslash [ \ ] which is the escape character for python. If you use it alone at the end of the line, it merges the line the slash is on and the next line into one statement. Like this

Ben Adams
  • 129
  • 1
  • 2
  • 11