9 Replies - 480 Views - Last Post: 08 September 2013 - 07:19 PM Rate Topic: -----

#1 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?

Sorry for the extra posting. Please close this thread.

Is This A Good Question/Topic? 0
  • +

Replies To: Division by Zero problem

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3721
  • View blog
  • Posts: 12,957
  • Joined: 12-December 12

Re: Division by Zero problem

Posted 08 September 2013 - 06:03 PM

View PostSandals456, on 09 September 2013 - 01:00 AM, said:

Sorry for the extra posting. Please close this thread.

I've closed the other two.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3721
  • View blog
  • Posts: 12,957
  • Joined: 12-December 12

Re: Division by Zero problem

Posted 08 September 2013 - 06:08 PM

View PostSandals456, 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

Was This Post Helpful? 0
  • +
  • -

#4 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • 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

Was This Post Helpful? 1
  • +
  • -

#6 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 Sandals456  Icon User is offline

  • New D.I.C Head

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

Re: Division by Zero problem

Posted 08 September 2013 - 06:18 PM

View PostValek, 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.
Was This Post Helpful? 0
  • +
  • -

#8 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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


Was This Post Helpful? 0
  • +
  • -

#9 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • 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

Was This Post Helpful? 1
  • +
  • -

#10 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1