2020-09-14 12:29:51 +02:00
|
|
|
from math import sqrt
|
|
|
|
|
2020-09-15 11:21:24 +02:00
|
|
|
|
2020-09-14 12:29:51 +02:00
|
|
|
def getValues():
|
|
|
|
while True:
|
2020-09-15 11:21:24 +02:00
|
|
|
values = input(
|
|
|
|
'Gi inn en andregradsliknings a, b og c separert med mellomrom:\n\t')
|
2020-09-14 12:29:51 +02:00
|
|
|
try:
|
|
|
|
splitValues = values.split(' ')
|
|
|
|
assert len(splitValues) == 3
|
2020-09-15 11:21:24 +02:00
|
|
|
return (
|
|
|
|
int(splitValues[0]),
|
|
|
|
int(splitValues[1]),
|
|
|
|
int(splitValues[2])
|
|
|
|
)
|
2020-09-14 12:29:51 +02:00
|
|
|
except ValueError:
|
|
|
|
print('Sørg for at alle tallene er heltall.\n')
|
|
|
|
except AssertionError:
|
|
|
|
print('Det skal bare være 3 tall.\n')
|
|
|
|
|
|
|
|
|
2020-09-15 11:21:24 +02:00
|
|
|
a, b, c = getValues()
|
|
|
|
d = b**2 - 4 * a * c
|
|
|
|
|
|
|
|
expression = f'{a}x^2 + {b}x + {c}'
|
2020-09-14 12:29:51 +02:00
|
|
|
if d > 0:
|
2020-09-15 11:21:24 +02:00
|
|
|
roots = [(-b + sqrt(d)) / (2 * a), (-b - sqrt(d)) / (2 * a)]
|
|
|
|
print(
|
|
|
|
f'Andregradsligningen {expression} har de to reelle løsningene {roots[0]} og {roots[1]}'
|
|
|
|
)
|
2020-09-14 12:29:51 +02:00
|
|
|
elif d == 0:
|
2020-09-15 11:21:24 +02:00
|
|
|
root = (-b + sqrt(d)) / (2 * a)
|
2020-09-14 12:29:51 +02:00
|
|
|
print(f'Andregradsligningen {expression} har en reell dobbelrot {root}')
|
|
|
|
else:
|
|
|
|
print(f'Andregradsligningen {expression} har to imaginære løsninger')
|