Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
print("Program to find roots of a quadratic equation.")
a = float(input("Enter 1st coefficient of equation(a) : "))
b = float(input("Enter 2nd coefficient of equation(b) : "))
c = float(input("Enter 3rd coefficient of equation(c) : "))
d = (b**2)-(4*(a*c))
if d > 0 or d < 0:
d = d**(1/2)
x1 = ((-b + d) / 2) <- problem 1
x2 = ((-b + d) / 2) <- problem 2
print("Coefficients of equation : ",x1,"and",x2)
else:
x = (-b/(2*a)) <- unnecessary
print("Coefficients of equation : ",x)
I am getting this as output:
Program to find roots of a quadratic equation.
Enter 1st coefficient of equation(a) : 5
Enter 2nd coefficient of equation(b) : 5
Enter 3rd coefficient of equation(c) : 5
Coefficients of equation : (-2.4999999999999996+4.330127018922194j) and (-2.4999999999999996+4.330127018922194j)
Process finished with exit code 0
]
solve for the discriminate. notice your x1 and x2 have the same equation. this is the quadric function. I corrected the formula.
import math
a=100
b=200
c=10
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
x1 = (-b-math.sqrt(d))/(2*a)
x2 = (-b+math.sqrt(d))/(2*a)
print("Coefficients of equation {x1} and {x2}".format(x1=round(x1,2),x2=round(x2,2)))
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
def eqQuadratic(Variables):
valuesoflinear=Variables[1]
a = float(valuesoflinear[0])
b = float(valuesoflinear[2])
c = float(valuesoflinear[4])
x = float(valuesoflinear[6])
y = a*x**2 + b*x + c
return y
input2 = input("input a, x, b, c to calculate the quadratic equation ")
Variables = ("The input for quadratic equation are ", input2)
y = eqQuadratic(Variables)
print("the answer for quadratic equation is ", y)
If I input all positive numbers it works, but if I input negative numbers, it won't works.
I get this error
Traceback (most recent call last):
File "C:/Users/maxime/Desktop/SESAME/PycharmProjects/Neocortex0.0/dj.py", line 14, in
y = eqQuadratic(Variables)
File "C:/Users/maxime/Desktop/SESAME/PycharmProjects/Neocortex0.0/dj.py", line 3, in eqQuadratic
a = float(valuesoflinear[0])
ValueError: could not convert string to float: '-'
How can I have the 4 different values from the input()command and convert them into float number.
The error is that you don't ask for 4 number. so the valuesoflinear[0] is just a sign -. You need to ask 4 number that can be negartive like -1
here a solution :
def eqQuadratic(Variables):
a = float(Variables[0])
b = float(Variables[1])
c = float(Variables[2])
x = float(Variables[3])
y = a * x ** 2 + b * x + c
return y
inputa = input("input a to calculate the quadratic equation ")
inputx = input("input x to calculate the quadratic equation ")
inputb = input("input b to calculate the quadratic equation ")
inputc = input("input c to calculate the quadratic equation ")
# Variables = ("The input for quadratic equation are ", input2)
y = eqQuadratic([inputa,inputx,inputb,inputc])
print("the answer for quadratic equation is ", y)
You can ask for several input in a row but you have to be careful if less or more than 4 parameters are enter
def eqQuadratic(Variables):
a = float(Variables[0])
b = float(Variables[1])
c = float(Variables[2])
x = float(Variables[3])
y = a * x ** 2 + b * x + c
return y
input2 = input("input a, x, b, c to calculate the quadratic equation ").split()
# Variables = ("The input for quadratic equation are ", input2)
if not len(input2) == 4:
print('you need to enter 4 param')
else:
y = eqQuadratic(input2)
print("the answer for quadratic equation is ", y)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So i have this task, i need to create formula in python, I have all the program running good, but i just can't think of the formula to count years, that's compound interest
what i need to count is how many years will it take to get to the target(lets say from 1500 to 2000)
Got this formula t = ln(A/P) / n[ln(1 + r/n)], but i don't get the right answer
https://www.thecalculatorsite.com/articles/finance/compound-interest-formula.php
Also tried this
https://www.algebra.com/algebra/homework/logarithm/logarithm.faq.question.117944.html
update.
Thanks for help! answer in comments for those who have same issue
This might work:
from math import log
p_start = 1500 # Beginning principal
r = 4.3 # Interest rate
p_end = 2000 # Ending principal
"""
# Lambda function
# n == number of compounding periods per year.
# Default n is 12, or monthly compounding
# Formula Breakdown
round((
log(A / P) /
(n * (
log(1 + (r/n))
)
)
# Add 0.5 to account for needing to round up.
) + 0.5
# Round to zero decimal places
, 0)
"""
get_time = lambda A, P, r, n=12: round((log(A / P) / (n * (log(1 + (r/n))))) + 0.5, 0)
# If interest rate isn't a percentage, convert it
if r > 0:
r /= 100
get_time(p_end, p_start, r, 1) # 7.0 using n = 1, or compounded once per year.
EDIT: Adding solution for comment below:
def calculate_years_to_target(principal, rate, target, n=12):
if rate > 0:
rate /= 100
years = round((math.log(target / principal) / (n * (math.log(1 + (rate/n))))) + 0.5, 0)
return years
calculate_years_to_target(1500, 4.3, 2000, 1)
Compound Interest Calculator:
import math
P = float(input("Enter the initial deposit: "))
A = float(input("Enter the final amount: "))
N = float(input("Enter the number of times the interest is applied per time period: "))
T = float(input("Enter the number of time periods elapsed: "))
interestRate = N*(pow(A/P, 1/(N*T))-1)*100
print("The interest rate is " + str(interestRate) + "%.")
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
#HCF
#input
C = int(input("the bigger number:" ))
D = int(input("the smaller number:" ))
#division
N = "="
M = "x"
A = "+"
#i don't know if I can add this to the while loop
Q = C//D
S = C%D
print (C,N,D,M,Q,A,S)
E = S
s = D
D = C
#Euclid's division algorithm
while S != 0:
Q = s//E
S = s%E
print(s, N,E, M, Q, A, S)
s = E
if S == 0:
print ("HCF =",E)
else :
E = S
is there a better way of writing this ?
if there is a syntax I am using incorrectly pls tell.
I don't know why I can't post this it's showing your post is mostly code pls explain ignore this last part it's only so this problem goes away.
Here's another way of writing your code
Corrects an error in posted code which provides incorrect results
Uses variables from an algorithm to provide a reference for variable names
Uses string interpolation to show formula
Euclidean Algorithm
Reference
Implementation
a, b, q, r corresponds to a, b, qi, ri in the algorithm
def euclidean_algorithm(a, b):
" Displays iterations of the euclidean algorithm "
if b > a:
# swap if necessary
# to ensure a is the larger number
a, b = b, a
while b:
q = a // b
r = a - q*b
print(f'{a} = {q} x {b} + {r}') # use string interpolation to show formula
a, b = b, r
print(f'HCF = {a}')
return a
Usage
a = int(input('First number: '))
b = int(input('Second number: '))
euclidean_algorithm(a, b)
Output
First number: 498
Second number: 222
498 = 2 x 222 + 54
222 = 4 x 54 + 6
54 = 9 x 6 + 0
HCF = 6
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am making mortgage calculator, I have provided information for variables p, i, n, but get error in the equation.
p[i(1 + i) ^ n] / [(1 + i) ^ n – 1]
What you shared is not valid python code. Here is an example of code that will accomplish what you are asking:
# define function:
def CalculateMortgage(p, i, n):
# calculate numerator:
numerator = p * (i *(1+i) ** n)
# calculate denominator:
denominator = ((1+i) ** n - 1)
# calculate mortgage:
mortgage = numerator/denominator
# return result:
return mortgage
# set variables:
p = 1
i = 1
n = 1
# call function:
mortgage = CalculateMortgage(p, i, n)
# print result:
print('Your mortgage is: ' + str(mortgage))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to make a program that allow me to enter a height of a ball to be dropped and put in the number of bounces and give me the total distance. I am having trouble with my equation and see some in-sight please.
h = int(input("Enter hieght"))
b = int(input("Enter Number of Bounces"))
for eachPass in range (b):
d = h * 0.6
if b >= 1:
bounce1 = h + d
if b >= 2:
bounce2 = (d * 0.6) + bounce1 + d
print (bounce2)
While I can't be sure, I believe you want something like this instead
h = int(input("Enter height"))
b = int(input("Enter Number of Bounces"))
bounce1 = 0
bounce2 = 0
d = h * 0.6
for eachPass in range(b):
if eachPass == 1:
bounce1 = h + d
if eachPass == 2:
bounce2 = bounce1 + h + d
print str(bounce2)