Bottom Page

Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 SyntaxError: unexpected character after line continuation character
Hello all, and I'm a new member here Big Grin .
I made a esoteric version of Python for fun, but I'm having some problems when running multiline code with the \n and \t characters. Here is the program that runs the code:
def decode(string):
    re = ""
    slist = string.split("_")
    for i in range(0,len(slist)):
        if slist[i][0] in "0123456789":
            if int(slist[i]) > 92:
                re = re + " "
                re = re + char[int(slist[i])]
        elif slist[i] not in char:
            re = re + slist[i]
    return re
char = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789~!@#$%^&*()_+-=[]{}\|;:,./<>? "
code = raw_input()
c = (decode(code))
Basically, it is Python but every character is mapped to the string "char" as seen above. Not sure how to explain this though. For example, a simple "Hello World!" program looks like this:
It separates the string by the "_" character, and gets the nth char in the char string. For example, the number "9" corresponds to the letter "p" in the char string, the number 3 to "r", and so on, and characters that aren't numbers are unchanged.

But I have trouble when entering this, which is an infinite counter:
Which corresponds to the normal Python:
i = 0
while True:
   i += 1
But for some reason it gives the following error:
Traceback (most recent call last): File "C:\Users\sakaf\Documents\Saka\scripts\Not Golly\", line 17, in <module> exec(c) File "<string>", line 1 i = 0\nwhile True:\n\tprint(i)\n\ti += 1 ^ SyntaxError: unexpected character after line continuation character
Strangely enough, when I paste i = 0\nwhile True:\n\tprint(i)\n\ti += 1 and run it using exec() in the python shell, it runs fine. So why is this happening?

Thanks Big Grin
That's some fun script.

To understand the problem you should print variable c.You'll see that i = 0\nwhile True:\n\tprint(i)\n\ti += 1 is printed which implies that the backslash has already been escaped. So your real string actually is i = 0\\nwhile True:\\n\\tprint(i)\\n\\ti += 1.

To solve the problem convert string to bytes and decode with unicode-escape

c = (decode(code))
c = c.encode('ascii').decode('unicode-escape')
More on problem:
>>> a = 'hello \nThis is newline \n\tThis is horizontal tab'
>>> print(a)
This is newline
        This is horizontal tab
>>> a = 'hello \\nThis is not a newline \\n\\tThis is not a horizontal tab'
>>> print(a)
hello \nThis is not a newline \n\tThis is not a horizontal tab
Saka likes this post
(Sep-25-2017, 02:28 PM)hbknjr Wrote: ...
To solve the problem convert string to bytes and decode with unicode-escape
Thank you so much! Now it works great and I can run pretty much any piece of python code in this obscure language. Thanks!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  loop only prints last character. mcmxl22 1 94 Feb-17-2020, 02:36 AM
Last Post: menator01
  create a list of object with a list of character studenthch 0 63 Feb-12-2020, 08:43 AM
Last Post: studenthch
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 6,052 Feb-08-2020, 06:58 PM
Last Post: karkas
  convert a character to numeric and back Skaperen 2 149 Jan-28-2020, 09:32 PM
Last Post: Skaperen
  Regex won't replace character with line break Tomf96 2 131 Jan-12-2020, 12:14 PM
Last Post: Tomf96
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 151 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  How to get the index of a character from a string chihaya 1 135 Dec-03-2019, 12:54 PM
Last Post: buran
  Character Encodings Evil_Patrick 1 143 Nov-17-2019, 02:23 PM
Last Post: snippsat
  Substitution with regular expression returns hidden character SOH bajacri 2 201 Nov-17-2019, 03:38 AM
Last Post: bajacri
  Turkish Character Prohibition squad 3 198 Nov-06-2019, 10:05 PM
Last Post: squad

Forum Jump:

Users browsing this thread: 1 Guest(s)