user17067132
user17067132

Reputation:

What is the reason for an IndentationError in my code?

i have python code like this, basically i just want to solve this product percentage so the result in product percentage will appear, here's the code:

product_a_sales = 5
product_b_sales = 5
total_sales = product_b_sales - product_a_sales
try:
product_a_percentage_sales=(product_a_sales/total_sales) * 100
except ZeroDivisionError:
    product_a_percentage_sales=0

and it returns an error like this

 File "<ipython-input-30-aa369d387f3d>", line 5
    product_a_percentage_sales=(product_a_sales/total_sales) * 100
                             ^
IndentationError: expected an indented block

Upvotes: 0

Views: 52

Answers (3)

Rouhollah Joveini
Rouhollah Joveini

Reputation: 158

As the Error message explains, in line 5, you have syntax error related to incorrect indentation. Frome more info on indentation in Python, please refer to these links: 1, 2, 3 and 4.

The correct syntax would be as follows:

product_a_sales = 5
product_b_sales = 5
total_sales = product_b_sales - product_a_sales
try:
    product_a_percentage_sales=(product_a_sales/total_sales) * 100
except ZeroDivisionError:
    product_a_percentage_sales=0

Upvotes: 0

tylerjames
tylerjames

Reputation: 123

You have to just watch your whitespace and indentations on the code after try:

    product_a_sales = 5
    product_b_sales = 5
    total_sales = product_b_sales - product_a_sales
    try:
        product_a_percentage_sales=(product_a_sales/total_sales) * 100
    except ZeroDivisionError:
        product_a_percentage_sales=0

Upvotes: 0

shadowtalker
shadowtalker

Reputation: 13903

This is a basic syntax error.

The statements between try and except must be indented.

The error message actually explains it perfectly: the line with product_a_percentage_sales = is not an "indented block", but an indented block was expected.

Refer to the Python tutorial for more information: 8. Handling Errors.

Upvotes: 1

Related Questions