# Division by Zero problem

Page 1 of 1

## 9 Replies - 343 Views - Last Post: 08 September 2013 - 07:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=328668&amp;s=0af47d51043dd19343cbe91956e935b5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

# Division by Zero problem

Posted 08 September 2013 - 06:00 PM

Hello all,

I need help with an assignment in which I have the user input two integers which will then perform division. If the numerator is 0, then it should print out 'INF', and same goes for the denominator.

For example:

a = 71
b = 4
17.75
0.056338028169014086
================================

a = 0
b = 4
0.0
INF
================================

a = 4
b = 0
INF
INF

This is my code so far:
```#Calling out variables
numerator_int = input("a = ")
denominator_int = input("b = ")
#The operands for the two user inputs
quotient1_int = float(numerator_int)/float(denominator_int)
quotient2_int = float(denominator_int)/float(numerator_int)

#Numerator if
if numerator_int == float(0):
print numerator_int
print("INF")
#Denominator if
elif denominator_int == float(0):
print ("INF")
print denominator_int
#If both numerator and denominator is zero
elif numerator_int and denominator_int == float(0):
print ("INF")
print ("INF")

else:
print quotient1_int
print quotient2_int

```

When I run the code and input zero to the numerator or denominator, I get the error code claiming that when either number is on the denominator, it doesn't exist since dividing by zero is impossible.

How would I be able to overcome this?

Is This A Good Question/Topic? 0

## Replies To: Division by Zero problem

### #2 andrewsw

• Fire giant boob nipple gun!

Reputation: 2884
• Posts: 9,566
• Joined: 12-December 12

## Re: Division by Zero problem

Posted 08 September 2013 - 06:03 PM

Sandals456, on 09 September 2013 - 01:00 AM, said:

I've closed the other two.

### #3 andrewsw

• Fire giant boob nipple gun!

Reputation: 2884
• Posts: 9,566
• Joined: 12-December 12

## Re: Division by Zero problem

Posted 08 September 2013 - 06:08 PM

Sandals456, on 09 September 2013 - 01:00 AM, said:

This is my code so far:
```...
quotient1_int = float(numerator_int)/float(denominator_int)
quotient2_int = float(denominator_int)/float(numerator_int)

```

You should check whether either of these values is 0 before attempting to divide them.

Please note that you should provide the full error message when posting. The error includes important information - in particular, the line number that caused the error.

This post has been edited by andrewsw: 08 September 2013 - 06:10 PM

### #4 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

## Re: Division by Zero problem

Posted 08 September 2013 - 06:11 PM

Thank you.

I have been wondering if there is a way to "void", if you will, either quotient operations when the denominator is zero, whether it be the numerator_int or denominator_int.

Would I have to put the operands after the if statements?

This post has been edited by andrewsw: 08 September 2013 - 06:11 PM
Reason for edit:: Removed quote

### #5 Valek

• The Real Skynet

Reputation: 541
• Posts: 1,708
• Joined: 08-November 08

## Re: Division by Zero problem

Posted 08 September 2013 - 06:11 PM

Fortunately, this one should be quite easy to fix. Looks like what you're encountering is this:

Quote

ZeroDivisionerror: integer division or modulo by zero

Using a try/except block for this would resolve the issue quite nicely.

```#Calling out variables
numerator_int = input("a = ")
denominator_int = input("b = ")

#The operands for the two user inputs
try:
quotient1_int = float(numerator_int)/float(denominator_int)
except ZeroDivisionerror:
quotient1_int = "INF"
try:
quotient2_int = float(denominator_int)/float(numerator_int)
except ZeroDivisionerror:
quotient2_int = "INF"

print quotient1_int
print quotient2_int
```

This way, you let your exception handling take care of the lifting for you.

One thing you may not have considered though: How do you want to handle the condition where someone inputs a text string instead of a number?

EDIT: 'Error' in 'ZeroDivisionerror' should have a capital E, but it is being changed when my post is submitted.

This post has been edited by Valek: 08 September 2013 - 06:13 PM

### #6 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

## Re: Division by Zero problem

Posted 08 September 2013 - 06:12 PM

This is my error message

Traceback (most recent call last):
File "C:", line 6, in <module>
quotient2_int = float(denominator_int)/float(numerator_int)
ZeroDivisionerror: float division by zero

### #7 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

## Re: Division by Zero problem

Posted 08 September 2013 - 06:18 PM

Valek, on 08 September 2013 - 07:11 PM, said:

Using a try/except block for this would resolve the issue quite nicely.

This way, you let your exception handling take care of the lifting for you.

One thing you may not have considered though: How do you want to handle the condition where someone inputs a text string instead of a number? />/>/>

EDIT: 'Error' in 'ZeroDivisionerror' should have a capital E, but it is being changed when my post is submitted.

Well the assignment is based on practicing the use if, elif, and else statements.

### #8 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

## Re: Division by Zero problem

Posted 08 September 2013 - 07:06 PM

This is what I have now:

```numerator_int = input("a = ")
denominator_int = input("b = ")

if denominator_int != 0:
quotient1_int = float(numerator_int)/float(denominator_int)
elif numerator_int != 0:
quotient2_int = float(denominator_int)/float(numerator_int)
elif (numerator_int == 0) and (denominator_int == 0):
print ("INF")
print ("INF")

if numerator_int == float(0):
print quotient1_int
print("INF")

elif denominator_int == float(0):
print ("INF")
print quotient2_int

elif numerator_int and denominator_int == float(0):
print ("INF")
print ("INF")

else:
print quotient1_int
print quotient2_int

```

Now the only problem I have is that when both the numerator and denominator are zero, I get an error saying the quotient1_int is not defined because of the if and elif statement in lines 4 and 6.

The outcome is:

```a = 0
b = 4
0.0
INF
>>> ================================ RESTART ================================
>>>
a = 4
b = 0
INF
0.0
>>> ================================ RESTART ================================
>>>
a = 0
b = 0
INF
INF

Traceback (most recent call last):
File "C:", line 15, in <module>
print quotient1_int
NameError: name 'quotient1_int' is not defined

```

### #9 Valek

• The Real Skynet

Reputation: 541
• Posts: 1,708
• Joined: 08-November 08

## Re: Division by Zero problem

Posted 08 September 2013 - 07:16 PM

Instead of printing INF on lines 9 and 10, why not set quotient1_int and quotient2_int to "INF" there instead? Then you can simply print it, you're using if and elif (and else, because if those first two aren't met, they're both confirmed as zero anyway, so the last elif there is redundant because else would serve the same purpose and be faster).

```numerator_int = input("a = ")
denominator_int = input("b = ")

if denominator_int != 0:
quotient1_int = float(numerator_int)/float(denominator_int)
elif numerator_int != 0:
quotient2_int = float(denominator_int)/float(numerator_int)
else:
quotient1_int = "INF"
quotient2_int = "INF"

print quotient1_int
print quotient2_int
```

### #10 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

## Re: Division by Zero problem

Posted 08 September 2013 - 07:19 PM

Here is what I have now

```
numerator_int = input("a = ")
denominator_int = input("b = ")

if denominator_int != 0:
quotient1_int = float(numerator_int)/float(denominator_int)
elif numerator_int != 0:
quotient2_int = float(denominator_int)/float(numerator_int)
#The value of quotient1_int and quotient2_int is now INF
elif (numerator_int == 0) and (denominator_int == 0):
quotient1_int = ("INF")
quotient2_int = ("INF")

if numerator_int == float(0):
print quotient1_int
print("INF")

elif denominator_int == float(0):
print ("INF")
print quotient2_int

elif numerator_int and denominator_int == float(0):
print ("INF")
print ("INF")

else:
print quotient1_int
print quotient2_int

```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }