带有克莱默法则的python线性方程 [英] python linear equation with Cramer's rule
问题描述
我是python的新手,正尝试使用Cramer规则编写线性方程.我已经输入了公式,并且有代码提示用户输入a,b,c,d,e和f,但是我的代码出现语法错误.我想修复代码,并拥有一个用于研究和纠正未来错误的系统.
I'm new to python and attempting to write a linear equation using Cramer's Rule. I've entered the formula and have code prompting user to enter a,b,c,d,e and f but my code is getting a syntax error. I'd like to fix the code and also have a system for researching and correcting future errors.
a,b,c,d,e,f = float(input("Enter amount: ")
a*x + by = e
cx + dy = f
x = ed-bf/ad-bc
y=af-ed/ad-bc
if (ad - bc == 0)print("The equation has no solution")
else print ("x=" x, "y=" y,)
推荐答案
基本上,您的代码是一个巨大的语法错误.请阅读一些基本教程,如评论中所建议.希望这会对学习过程有所帮助(我没有遍历实际的数学公式):
Basically, your code was one giant syntax error. Please read some basic tutorial, as was suggested in the comments. Hopefully this will help in the learning process (I didn't go through the actual math formulas):
a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))
d = float(input("Enter d: "))
e = float(input("Enter e: "))
f = float(input("Enter f: "))
##a*x + by = e
##cx + dy = f
if (a*d - b*c == 0):
print("The equation has no solution")
else:
x = (e*d-b*f)/(a*d-b*c)
y = (a*f-e*d)/(a*d-b*c)
print ("x=%s" % x, "y=%s" % y)
您必须在要相乘的数字之间放置*
.您的输入语句中缺少一个括号.方程本身被注释掉了,因为否则Python将其当作代码(错误地编写了一个).您必须将分母用括号括起来,因为数学.您必须以:
结尾if
,else
,elif
,for
,while
等.缩进在Python中非常重要.
You have to put *
between the numbers you want to multiply. You had one parenthesis missing from your input statement. The equations themselves are commented out, because otherwise Python takes them as a code (incorrectly written one). You have to enclose the denominator in parentheses because math. You have to end the if
, else
, elif
, for
, while
and such with :
. Indentation is VERY important in Python.
这篇关于带有克莱默法则的python线性方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!