-2

i just wondering, i build the code to ignore the zero division like this, since im new in python here's my 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

but eventually, it return an error :

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

and im stuck with this error. Thank you.

robery
  • 61
  • 4

2 Answers2

2

IndentationError is just as it says: an error in the indentation of your code. The line given must be indented. Python uses indentation to tell when blocks of code start and end. You should have something like

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
BareNakedCoder
  • 3,257
  • 2
  • 13
  • 16
-1

I think you should have a 4 spaces before this line

 product_a_percentage_sales = (product_a_sales/total_sales) * 100
MexcelsiorB
  • 29
  • 1
  • 5
  • Don't use single-space indentation. Use 4 spaces, [as PEP 8 recommends](https://peps.python.org/pep-0008/#indentation) and is standard. Or use a different number if your style guide recommends it, but I'm not aware of any that recommend 1. – wjandrea Apr 22 '22 at 02:52
  • i want to mean spaces no one space – MexcelsiorB Apr 22 '22 at 03:18