'How to fix File "<tokenize>", line 3 name = get_text() ^ IndentationError: unindent does not match any outer indentation level in my Al project

I'm working on an AL project for my school. I wrote the codes by Python on Jupyter notebook. When I ran it, an error has appeared.You can see it below

Here is the real code:

def assistant():
speak("Xin chào, bạn tên là gì nhỉ?")
name = get_text()
if name:
    speak("Chào bạn {}".format(name))
    speak("Bạn cần Bot Alex có thể giúp gì ạ?")
    while True:
        text = get_text()
        if not text:
            break
        elif "dừng" in text or "tạm biệt" in text or "chào robot" in text or "ngủ thôi" in text:
            stop()
            break
        elif "có thể làm gì" in text:
            help_me()
        elif "chào trợ lý ảo" in text:
            hello(name)
        elif "hiện tại" in text:
            get_time(text)
        elif "mở" in text:
            if 'mở google và tìm kiếm' in text:
                open_google_and_search(text)
            elif "." in text:
                open_website(text)
            else:
                open_application(text)
        elif "email" in text or "mail" in text or "gmail" in text:
            send_email(text)
        elif "thời tiết" in text:
            current_weather()
        elif "chơi nhạc" in text:
            play_song()
        elif "hình nền" in text:
            change_wallpaper()
        elif "đọc báo" in text:
            read_news()
        elif "định nghĩa" in text:
            tell_me_about()
        else:
            speak("Bạn cần Bot giúp gì ạ?")

Here is the error

File "<tokenize>", line 3
name = get_text()
^IndentationError: unindent does not match any outer indentation level


Solution 1:[1]

The problem is that each block of Python code must be indented at a certain number of spaces, so if you have code like this:

if test:
    do_thing()
else:
    do_other_thing()

The first characters of "if test:" and "else:" must have exactly the same number of spaces to their left. Both the "i" and "e" characters should align vertically, and you should either use tabs or spaces, but you shouldn't mix them. (By convention, "do_thing()" and "do_other_thing()" should align with each other as well, but the compiler doesn't strictly require it because they are in separate blocks.) When copying code via cut-and-paste, it's very easy to mess this up: Either tabs get converted to space, or vice-versa, or you didn't highlight one of the space characters, or already had a space character that's present.

The particular message you have, "^IndentationError: unindent does not match any outer indentation level" says that on line 3 (the "name = get_text()" line), the left-most character is in an incorrect, confusing position.

From looking at your code, name = get_text() is probably supposed to start in the same column as speak("Xin chào, b?n tên là gì nh??"), and both are probably supposed to be more indented than def assistant():

Try adding spaces to the start of the speak("Xin chào, b?n tên là gì nh??") line until it matches the indentation of the next few lines, and that all of them are a few columns further to the right than def assistant():

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1