Python Forum

Full Version: IndentationError: unexpected indent
You're currently viewing a stripped down version of our content. View the full version with proper formatting.


Hello Team,

I am currently taking a course on skillshare for Python. I am following along, but one example I keep get unexpected indent error.

This is the code I have:

in_stock = ["blue pens", "paper", "staples"]

shopping_cart = ["blue pens", "paper", "staples", "orange post-its"]

for item in shopping_cart:
    if item in_stock:
        print("Adding " + item + " to your order.")
    else:
        print("Sorry " + item + " is not in stock.")
I do not quite understand what the issue is as if and else are indented 4 spaces. print is indented a total of 8 spaces.

Anyone know what im doing wrong?
Change
    if item in_stock:
to
    if item in in_stock:
Thank you heiner55. That fixed it!

I am confused tho as to why the error was considered indentation error. I did not focus on anything else but indentation, which was my mistake. Do you know why it is considered an indentation error?
I got a syntax error.
Thanks Heiner55. Not sure why the error message for me was different, but it is resolved now. I will pay more attention to the syntax.
Maybe you had tabs mixed with spaces.
I would probably not call a variable in_something for that reason alone
Error:
File "tester.py", line 7 if item in_stock: ^ SyntaxError: invalid syntax
Although you are allowed to name a variable anything in python, we prefer to restrict certain ones... such as no built-in's or single char names , CamelCase class names, capped GLOBALS, etc. I would probably add to the list things like in_var as when you read a if header, it suppose to be English readable
Quote:if item in in_stock:
and a double in is not readable as well as obviously causing syntax bugs

Im not sure why you got an indentation error. I would also never mix tabs and spaces. Convert all copied code to 4 spaces as that is the norm.

Also use the format instead of concatenation
https://python-forum.io/Thread-Basic-str...xpressions