英文:
Quadratic equation in Python
问题
这是我的代码。有人可以帮忙解决问题吗?在下面的代码中,我做了一切,但它不起作用。
def read_equation(textfile: str) -> str:
with open(textfile, 'r') as file:
equation = file.read()
return equation
def get_coefficients(input_str: str, coef: str) -> int:
for i in input_str.split():
if i.endswith(coef):
try:
value = i.replace(coef, "", 1)
if not value or value == "+":
value = "1"
elif value == "-":
value = "-1"
return int(value)
except ValueError as err:
print(err)
raise TypeError(f"其他符号 {coef},不是整数。")
查找系数
def get_a_b_c(input_str: str) -> tuple:
a = get_coefficients(input_str, "x^2")
b = get_coefficients(input_str, "x")
c = get_coefficients(input_str, "")
return a, b, c
查找判别式
def solve_quadratic_equation(a: int, b: int, c: int) -> str:
d = b ** 2 - 4 * a * c
if d > 0:
x1 = (-b + d ** 0.5) / (2 * a)
x2 = (-b - d ** 0.5) / (2 * a) # 找到根
roots = f"x1 = {x1}\nx2 = {x2}"
elif d == 0:
x = -b / (2 * a)
roots = f"x = {x}"
else:
roots = "无根"
return roots
根写入文件
def write_roots(output: str, textfile: str) -> None:
with open(textfile, 'a') as file:
file.write('\n')
file.write(output)
主函数
def main():
user_input = read_equation(textfile="equation.txt")
coefficients = get_a_b_c(user_input)
roots = solve_quadratic_equation(*coefficients)
write_roots(output=roots, textfile="equation.txt")
if __name__ == "__main__":
main()
我尝试获取根并将其写入现有文件,但它不起作用。
英文:
here is my code. Can anybody help to solve the problem? In the code below everything what I made but it doesn't work.
def read_equation(textfile: str) -> str:
with open(textfile, 'r') as file:
equation = file.read()
return equation
def get_coefficients(input_str: str, coef: str) -> int:
for i in input_str.split():
if i.endswith(coef):
try:
value = i.replace(coef, "", 1)
if not value or value == "+":
value = "1"
elif value == "-":
value == "-1"
return int(value)
except ValueError as err:
print(err)
raise TypeError(f"Other symbol {coef}, not an int.")
Finding coefficients
def get_a_b_c(input_str: str) -> tuple:
a = get_coefficients(input_str, "x^2")
b = get_coefficients(input_str, "x")
c = get_coefficients(input_str, "")
return a, b, c
finding discriminant
def solve_quadratic_equation(a: int, b: int, c: int) -> str:
d = b ** 2 - 4 * a * c
if d > 0:
x1 = (-b + d ** 0.5) / (2 * a)
x2 = (-b - d ** 0.5) / (2 * a) # finding roots
roots = f"x1 = {x1}\nx2 = {x2}"
elif d == 0:
x = -b / (2 * a)
roots = f"x = {x}"
else:
roots = "No roots"
return roots
roots in file
def write_roots(output: str, textfile: str) -> None:
with open(textfile, 'a') as file:
file.write('\n')
file.write(output)
main function:
def main():
user_input = read_equation(textfile="equation.txt")
coefficients = get_a_b_c(user_input)
roots = solve_quadratic_equation(*coefficients)
write_roots(output=roots, textfile="equation.txt")
if __name__ == "__main__":
main()
I tried to get roots and write it in existing file, but it doesn't work
答案1
得分: 0
Here is the translated content:
事实上,你有问题的地方是:
c = get_coefficients(input_str, "")
这一行没有正确地获取常数的系数。
现在,如果你运行你的当前代码:
get_coefficients("1x^2 - 10x + 2 = 0")
你会得到:
----> 5 c = get_coefficients(input_str, "")
6 return a, b, c
C:\Users\NAVIGA~1\AppData\Local\Temp/ipykernel_27960/1668498165.py 中的 get_coefficients(input_str, coef)
11 except ValueError as err:
12 print(err)
---> 13 raise TypeError(f"Other symbol {coef}, not an int.")
TypeError: Other symbol , not an int.
解决方法:
你可以通过在方程周围使用 split(' =')
来获取 c 的系数。
def get_a_b_c(input_str: str) -> tuple:
a = get_coefficients(input_str, "x^2")
b = get_coefficients(input_str, "x")
c = int(input_str.split(' =')[0][-1]) # 从拆分中获取 c
return a, b, c
print(get_a_b_c("1x^2 - 10x + 2 = 0"))
#输出
(1, 10, 2)
现在,你可以使用你所有的公式:
def solve_quadratic_equation(a: int, b: int, c: int) -> str:
d = b ** 2 - 4 * a * c
if d > 0:
x1 = (-b + d ** 0.5) / (2 * a)
x2 = (-b - d ** 0.5) / (2 * a) # 寻找根
roots = f"x1 = {x1}\nx2 = {x2}"
elif d == 0:
x = -b / (2 * a)
roots = f"x = {x}"
else:
roots = "没有根"
return roots
coefficients = get_a_b_c("1x^2 - 10x + 2 = 0")
roots = solve_quadratic_equation(*coefficients)
print(roots)
#输出
'x1 = -0.2041684766872809\nx2 = -9.79583152331272'
英文:
Actually, you have problem with :
c = get_coefficients(input_str, "")
this line is not correctly fetching the coefficients for the constant.
Now, if you run your current code with this:
get_coefficients("1x^2 - 10x + 2 = 0")
you will get:
----> 5 c = get_coefficients(input_str, "")
6 return a, b, c
C:\Users\NAVIGA~1\AppData\Local\Temp/ipykernel_27960/1668498165.py in get_coefficients(input_str, coef)
11 except ValueError as err:
12 print(err)
---> 13 raise TypeError(f"Other symbol {coef}, not an int.")
TypeError: Other symbol , not an int.
Workaround:
You can get coefficient of c by using split(' =')
around the equation.
def get_a_b_c(input_str: str) -> tuple:
a = get_coefficients(input_str, "x^2")
b = get_coefficients(input_str, "x")
c = int(input_str.split(' =')[0][-1]) # getting c from split
return a, b, c
print(get_a_b_c("1x^2 - 10x + 2 = 0"))
#output
(1, 10, 2)
Now, you can use all your formulas:
def solve_quadratic_equation(a: int, b: int, c: int) -> str:
d = b ** 2 - 4 * a * c
if d > 0:
x1 = (-b + d ** 0.5) / (2 * a)
x2 = (-b - d ** 0.5) / (2 * a) # finding roots
roots = f"x1 = {x1}\nx2 = {x2}"
elif d == 0:
x = -b / (2 * a)
roots = f"x = {x}"
else:
roots = "No roots"
return roots
coefficients = get_a_b_c("1x^2 - 10x + 2 = 0")
roots = solve_quadratic_equation(*coefficients)
print(roots)
#output
'x1 = -0.2041684766872809\nx2 = -9.79583152331272'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论