EX23 LPTHW ex23 not working

I wrote the code exactly as it was but I’m getting an error and the code isn’t working
this is the error that I’m getting

I feel bad for not being able to solve it just by looking around since this is one of the main points of the book but I don’t wanna just skip it.

Hi @Joey have a look at line 15 (and the lines before and after) maybe you will spot the bug.

Otherwise post your code here like this. Put your code in code brackets like below.

your code

here is my code :

import sys
script, input_encoding, error = sys.argv

def main(language_file, encoding, errors):
    line = language_file.readline()

    if line:
        print_line(line, encoding, errors)
        return main(language_file, encoding, errors)

def print_line(line, encoding, errors):
    next_lang = line.strip()
    raw_bytes = next_lang.encode(encoding, errors=errors)
    cooked_string = raw_bytes.decode(encoding, errors=errors)

    print(raw_bytes, "<===>", cooked_string)

languages = open("languages.txt", encoding="utf-8")

main(languages, input_encoding, error)

hope it has nothing wrong cause I have gone over it lots of times even backwards as the book says.

Hey Joey, thanks for sharing your issue. I also had this problem previously, but mine was down to not typing in line 15 correctly. Your code looks the same as mine, but when I copied yours into my own text editor and then ran it, it didn’t appear to do anything.
Only thing I can see is that on line 18 print(raw_bytes. “<===>”, cooked_string) your print command isn’t shown in bold by the editor, whereas in my code, it is, and everything else bold in yours shows the same way in mine. Maybe it isn’t identifying print on line 18 as a command like import, def, if and return? They’re all highlighted bold in the editor, but it isn’t.

Just a thought,


Hmm. I copied your code an run it. Everything is fine.
Two hints:

  1. Do you use Python3.6 or higher?
    python3.6 test.py utf-8 strict
  2. Maybe it’s a Windows problem. I tested it on Linux.

Yes I’m using python 3.7, I also think it is a windows problem I’m running 8.1, should I try to fix it if it’s a windows problem and how to do so if I should fix it?

First, make sure that you really use Python3.7. I had some confusions earlier on with two installed version. Maybe you installed one months or years ago and don’t remember it.
Run your script like that:

python3.7 test.py utf-8 strict

If you still have the problem, I can’t help you. You have to copy the error message and paste it into google and search around. Good luck.

Hey @Joey, don’t feel bad at all. Programming sucks and is the most frustrating thing ever and then YOU FIX IT OH WOW YOU ARE A GOD and then it sucks again and it’s the most frustrating thing ever AND THEN YOU FIX IT YOU CAN DO ANYTHING IN THE WORLD and then…

In this case, you are most likely using a Windows computer that has a non-English default language. You clicked on the link for the languages.txt file, and Windows tried to “help” by converting my beautiful utf-8 encoded file into your non-English language encoding. Now when you tell it utf-8 in the program it barfs and you get this error.

Two ways to fix it:

  1. Specify your computer’s language encoding instead of utf-8 when you run it. This is the hardest.
  2. Delete languages.txt and then go back and instead of clicking on the file, right click and Save As… which seems to not mangle the file.

Thank you so much, I tried the second way and the file ran but not all the languages got identified.

That’s pretty normal. I’d say you solved this exercise and should move on.

1 Like

I don’t think there is anything wrong with the .py file. I would open languages.txt in notepad and check the encoding. Also, from powershell are you typing in python ex23.py utf-8 strict to match the command line arguments in the script?


I have been experiencing trouble when I run ex23. I am on windows and running python 3.6. My code is below:

import sys

script, input_encoding, error= sys.argv

def main(language_file, encoding, errors):

line= language_file.readline()


if line:

    print_line(line, encoding,errors)

    return main(language_file,encoding, errors)

def print_line(line, encoding, errors):

next_lang= line.strip()

raw_bytes= next_lang.encode(encoding, errors=errors) 

cooked_string= raw_bytes.decoded(encoding, errors= errors

print(raw_bytes, "<===>", cooked_string)

languages = open(“language.txt”, encoding= “utf-8”)

main(languages, input_encoding, error)

when I run the above in powershel I get the below error


Could you please assist me?

There’s a closing parenthesis missing at the end of line 14.

Thank you florian. Much appreciate the help.

You’re welcome, I’m glad I could help. :slight_smile:

For the future, if you get an error in a line that looks good, scrutinise the lines before. Especially syntax errors are often caused by little slips like this one.

1 Like
A free service run by Zed A. Shaw for learncodethehardway.org.