6 Respuestas
+ 5
For square root, you need only 1 operand (1 input number)
Calculation in Python is simple:
result = num1 ** 0.5
eg. Square root of 16.0 is 4.0
Good luck!
+ 1
Lol ok. Its been a while since I had to do algebra :)
Anyway the quadratic formula describes a parabola and if you solve for x, you can have two, one or zero solutions.
I would make sure that A cannot be zero, because that would cause zero division error. Or use try/except clause for the division. Then I would compute the discriminant first and store it in a variable.
discriminant = num2**2 - 4*num1*num3
If this is zero then you have only 1 solution. If it is positive then the solutions would be
result1 = (-num2 + discriminant**0.5)/(2*num1)
result2 = (-num2 - discriminant**0.5)/(2*num1)
If the discriminant is negative, then the solution is in the realm of complex numbers, not my cup of tea :p
Hope I got this right :)
+ 1
Math module from a standard library may be of use. Type:
import math
on top of your code to get access for a bunch of out-of box functions. You're probably looking for math.sqrt().
0
The code:
while True:
print("Options:")
print("Enter 'add' to add two numbers")
print("Enter 'subtract' to subtract two numbers")
print("Enter 'multiply' to multiply two numbers")
print("Enter 'divide' to divide two numbers")
print("Enter 'prgms' to open up program list")
print("Enter 'quit' to end the program")
user_input = input(": ")
if user_input == "quit":
break
elif user_input == "add":
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))
result = str(num1 + num2)
if (num1 + num2) > 100:
print("ERROR")
else:
print("The answer is " + result)
elif user_input == "subtract":
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))
result = str(num1 - num2)
if (num1 + num2) > 100:
print("ERROR")
else:
print("The answer is " + result)
elif user_input == "multiply":
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))
result = str(num1 * num2)
if (num1 + num2) > 100:
print("ERROR")
else:
print("The answer is " + result)
elif user_input == "divide":
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))
result = str(num1 / num2)
if (num1 + num2) > 100:
print("ERROR")
else:
print("The answer is " + result)
elif user_input == "prgms":
print("Enter 'sqrt'")
print("Enter 'exit'")
user_input = input(": ")
if user_input == "exit":
break
elif user_input == "sqrt":
num1 = float(input("Enter A value: "))
num2 = float(input("Enter B value: "))
num3 = float(input("Enter C value: "))
result = str(-num2 + (num2 - 4 * num1 * num3)/(num1 * 2))
print("The answer is " + result)
0
Thank you for the help with square rooting!
However, I now realize I wrote my question wrong. I wanted to know how to add the quadratic formula into Python 3, but I was so focused on the square root portion that I forgot to mention that I was trying to put it into the formula.
0
Everytime I add that, IDLE always says "multiple statements found while compiling a single statement"