I’m trying to calculate the zeros of a vertex form parabola. The code works when d is equals 0, but doesn’t otherwise.
a = float(input("Insert coefficient a: "))
h = float(input("Insert coefficient h: "))
k = float(input("Insert coefficient k: "))
d = -k/a
if d >= 0:
x_1 = h + math.sqrt(d)
x_2 = h - math.sqrt(d)
else:
x_1 = h + math.sqrt((-k/a))
x_2 = complex(h),-math.sqrt((-k/a))
if d > 0:
print("x\u2081 = ", x_1, " and x\u2082 = ", x_2)
elif d == 0:
print("x\u2081 = ", x_1, “. There is no x\u2082.”)
else:
print("Impossible with real numbers. ")
print("x\u2081 = ", x_1, " and x\u2082 = ", x_2)
import time
time.sleep(5)
import os
import sys
restart = input("\nDo you want to restart the program? [y/n] > ")
if restart == “y”:
os.execl(sys.executable, os.path.abspath(file), *sys.argv)
else:
print("\nThe program will be closed…")
sys.exit(0)
Can you show what does happen when you run it? Your code looks like it might work for d > 0. The concerns I have are:
Mixed quotes. All the quotes should be simple single ' or double ". In the middle of your paste, some are complex quotes ”. I don’t know if that’s an error with the paste or if that’s in your code. Python won’t interpret them as string delimiters though.
You create d = -k/a, but then in several other places you don’t bother to use it, continuing to use -k/a in the code. You should be consistent and use one or the other.
math.sqrt() can’t handle negative numbers to give complex answers. You probably want to be using cmath.sqrt() instead.
running an exec to restart the program is overkill (and done incorrectly). Much better to just make a simple loop. Even a while True:. Then break if the answer to restart isn’t yes.