0

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
shadowtalker
  • 12,529
  • 3
  • 53
  • 96
  • Does this answer your question? [Why am I getting "IndentationError: expected an indented block"?](https://stackoverflow.com/questions/4446366/why-am-i-getting-indentationerror-expected-an-indented-block) – Nick Apr 20 '22 at 04:15

3 Answers3

1

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.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96
0

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
tylerjames
  • 123
  • 8
0

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
Rouhollah Joveini
  • 158
  • 1
  • 3
  • 14