Python Calculator Divide By Zero/sqrting A Neg. Int. Crashing Program
Solution 1:
The problem is that you are trying to catch an exception before it is thrown. Instead, try this:
elif choice == "4":
numberA = raw_input("Enter your dividend: ")
numberB = raw_input("Enter your divisor: ")
whilefloat(numberB) == 0:
print"You cannot divide by zero. Please choose another divisor."
numberB = raw_input("Enter your divisor: ")
print"The quotient of those numbers is:"print division(numberA, numberB)
Solution 2:
Here's your answer without exception handling. Essentially you just make this questionable input at the beginning of an infinite loop, but break out when you find that the input is all good.
For your square root:
elif choice == "5":
whileTrue:
numberA = raw_input("Enter the number you wish to find the square root of: ")
iffloat(numberA) >= 0:
breakprint"Cannot take the square root of a negative number."print"Your result is:", sqrt(numberA)
and for division:
elif choice == "4":
numberA = raw_input("Enter your dividend: ")
whileTrue:
numberB = raw_input("Enter your divisor: ")
if numberB != "0":
breakprint"You cannot divide by zero. Please choose another divisor."print"The quotient of those numbers is:", division(numberA, numberB)
And to make your program stall before it closes:
elif choice == "6":
print"Goodbye! Thank you for your time spent both judging my project and those of everyone else! Have a nice day! (。◕‿◕。)"
raw_input('')
keepProgramRunning = False
Cheers!
Solution 3:
You should use exceptions, like you're already doing in convertString
.
try:
result = division(numberA, numberB)
print"The quotient of those numbers is:"print result
except ZeroDivisionError:
print"You cannot divide by zero. Please choose another divisor."
Solution 4:
Your format of the try:...except:
statement is wrong. Try this:
elif choice == "4":
numberA = raw_input("Enter your dividend: ")
numberB = raw_input("Enter your divisor: ")
print"The quotient of those numbers is:"try:
print division(numberA, numberB)
except ZeroDivisionError:
print"You cannot divide by zero. Please choose another divisor."
Solution 5:
This does not answer your coding question, but I think it is worth to comment.
sqrt: The square root of a negative number is an imaginary number. Python supports complex numbers with complex(real_part, imaginary_part)
.
so your sqrt calculation could be written as:
elif choice == "5":
numberA = raw_input("Enter the number you wish to find the square root of: ")
numberA = float(numberA)
if numberA < 0:
sqrt_numberA = complex(0, sqrt(abs(numberA)))
else:
sqrt_numberA = sqrt(numberA)
print"Your result is:", sqrt_numberA
division: A division by 0 is infinite. Python indicates infinite with 'inf'. In fact, it returns 'inf' with numbers higher than 1E309:
>>>1E309
inf
and the division of two of these numbers produces 'nan'
>>>1E309/1E309
nan
so that you could write:
elif choice == "4":
numberA = raw_input("Enter your dividend: ")
numberB = raw_input("Enter your divisor: ")
try:
div = division(numberA, numberB)
except ZeroDivisionError:
div = 'inf'print"The quotient of those numbers is:", div
this code should be additionally extended to return -inf, when numberA is negative, and to take into account situations such as 0/0
Post a Comment for "Python Calculator Divide By Zero/sqrting A Neg. Int. Crashing Program"