Division by Zero problem

Page 1 of 1

1 Replies - 144 Views - Last Post: 08 September 2013 - 06:00 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 - 05:59 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  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3526
  • View blog
  • Posts: 12,042
  • Joined: 12-December 12

Re: Division by Zero problem

Posted 08 September 2013 - 06:00 PM

Duplicate post closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1