python3代数表达式解x
我对python比较陌生,决定制作一个计算器。不幸的是,我不能解x。错误是: SyntaxError:只能将带星号的表达式用作赋值目标 我想让这个人输入问题,然后打印x,所以我想不出任何解决方法 请帮忙,提前谢谢你的帮助 我的代码:python3代数表达式解x,python,python-3.x,algebra,Python,Python 3.x,Algebra,我对python比较陌生,决定制作一个计算器。不幸的是,我不能解x。错误是: SyntaxError:只能将带星号的表达式用作赋值目标 我想让这个人输入问题,然后打印x,所以我想不出任何解决方法 请帮忙,提前谢谢你的帮助 我的代码: import random from datetime import datetime import time def ints(x,y): x = int(x) y = int(y) now = datetime.now() def solve(
import random
from datetime import datetime
import time
def ints(x,y):
x = int(x)
y = int(y)
now = datetime.now()
def solve(c, z):
c = (*z)
print(now.year)
time.sleep(1)
print("WELCOME TO THE JERAXXUS SOFTWARE")
time.sleep(2)
math = True
if math == True:
user_input = input("My name is jeraxxus, please put in 2 numbers followed by a operator, *, /, +, -, **, or %. No commas please")
user_input = str.split(user_input)
a_list = [user_input]
n1 = user_input[0]
n2 = user_input[1]
operate = user_input[2]
algebra = input("does your mathmatical equation contain algebraic values?")
if algebra == 'no':
if operate == '*':
n1 = int(n1)
n2 = int(n2)
print(n1 * n2)
elif operate == '/':
n1 = int(n1)
n2 = int(n2)
print(n1 / n2)
elif operate == '+':
n1 = int(n1)
n2 = int(n2)
print(n1 + n2)
elif operate == '-':
n1 = int(n1)
n2 = int(n2)
print(n1 - n2)
elif operate == '**':
n1 = int(n1)
n2 = int(n2)
print(n1 ** n2)
elif operate == '%':
n1 = int(n1)
n2 = int(n2)
print(n1 % n2)
elif operate != '%' and operate!= '**' and operate != '-' and operate != '+' and operate != '/' and operate != '*':
print("SHAME YOU SHOULD HAVE FOLLOWED MY COMMANDS")
math = False
elif algebra == 'yes':
problem = input("please state your algebraic problems with spaces after each operation and number, the order is crucial please have only 1 variable and have it first.")
problem = str.split(problem)
lop = problem[0]
b_list = [problem]
sovle(lop, b_list)
以下是关于您的代码的一些内容: 函数
ints
最后什么也不做,因为你不返回任何值,你得到的错误来自c=(*z)
你不能在赋值中这样做,但是你可以在函数调用中这样做fun(*argument_list)
此处使用的变量math
无效,因为您将其赋值为True并检查其值是否相同,因此您无条件地输入该if
块,这意味着不需要该if math==True
,也许你的意思是while math
,当变量math
为真时,重复该块
变量a_list
的原因是什么?您不使用它
在块中,如果代数=='no'
您可以先进行int转换,然后检查操作
,以避免重复相同的代码,说到operate
,最后一个elif
是多余的,因为如果你到了那里,那是因为它没有通过其他比较,因此无需再次检查所有可能性,将其更改为一个简单的,否则
通过这些小小的修改,您的代码将如下所示
import random
from datetime import datetime
import time
def solve(c, z):
raise NotImplementedError("In process of programming") # an error because there is still no code for this
now = datetime.now()
print(now.year)
time.sleep(1)
print("WELCOME TO THE JERAXXUS SOFTWARE")
time.sleep(2)
math = True
while math:
user_input = input("My name is jeraxxus, please put in 2 numbers followed by a operator, *, /, +, -, **, or %. No commas please")
user_input = str.split(user_input)
#a_list = [user_input]
n1 = user_input[0]
n2 = user_input[1]
operate = user_input[2]
algebra = input("does your mathematical equation contain algebraic values?")
if algebra == 'no':
n1 = int(n1)
n2 = int(n2)
if operate == '*':
print(n1 * n2)
elif operate == '/':
print(n1 / n2)
elif operate == '+':
print(n1 + n2)
elif operate == '-':
print(n1 - n2)
elif operate == '**':
print(n1 ** n2)
elif operate == '%':
print(n1 % n2)
else:
print("SHAME YOU SHOULD HAVE FOLLOWED MY COMMANDS")
math = False
elif algebra == 'yes':
problem = input("please state your algebraic problems with spaces after each operation and number, the order is crucial please have only 1 variable and have it first.")
problem = str.split(problem)
lop = problem[0]
b_list = [problem]
solve(lop, b_list)
def calculator():
exp = input("x= ")
print("x=", eval(exp) )
无代数部分工作正常,您现在需要计算解x部分,如果您也需要帮助,请询问:)
简单计算器
在快速搜索之后,使用eval
,用python制作一个简单的计算器很容易,如下所示
import random
from datetime import datetime
import time
def solve(c, z):
raise NotImplementedError("In process of programming") # an error because there is still no code for this
now = datetime.now()
print(now.year)
time.sleep(1)
print("WELCOME TO THE JERAXXUS SOFTWARE")
time.sleep(2)
math = True
while math:
user_input = input("My name is jeraxxus, please put in 2 numbers followed by a operator, *, /, +, -, **, or %. No commas please")
user_input = str.split(user_input)
#a_list = [user_input]
n1 = user_input[0]
n2 = user_input[1]
operate = user_input[2]
algebra = input("does your mathematical equation contain algebraic values?")
if algebra == 'no':
n1 = int(n1)
n2 = int(n2)
if operate == '*':
print(n1 * n2)
elif operate == '/':
print(n1 / n2)
elif operate == '+':
print(n1 + n2)
elif operate == '-':
print(n1 - n2)
elif operate == '**':
print(n1 ** n2)
elif operate == '%':
print(n1 % n2)
else:
print("SHAME YOU SHOULD HAVE FOLLOWED MY COMMANDS")
math = False
elif algebra == 'yes':
problem = input("please state your algebraic problems with spaces after each operation and number, the order is crucial please have only 1 variable and have it first.")
problem = str.split(problem)
lop = problem[0]
b_list = [problem]
solve(lop, b_list)
def calculator():
exp = input("x= ")
print("x=", eval(exp) )
有了它,您可以处理任何有效的python表达式,就好像您处于空闲状态一样
但是,如果出于学术原因,你不想使用它,那么正如我之前告诉你的,你必须制作一个数学表达式的解析器,识别其中的运算符,并根据其优先级排列它们,最后解决expresionpost full stacktrace.what is
c=(*z)
应该怎么做?请发布堆栈跟踪的全文。它应该指向一个特定的行。c=*z应该为答案分配一个变量,然后我打印variable@njzk2这一行是最后一行,解算函数。这就是我需要帮助的地方,很抱歉造成混乱,我正在处理ints函数,但我停了下来,忘了删除它。抱歉,OK,让我们集思广益:你想解什么样的方程?这:a=b*x+c
带有a,b,c编号,a=b op x op c
相同但带有op-one运算符,或带有a,b,c任何类型的不带x的表达式,或exp_const=exp_x
其中exp_const是任何表达式,只有编号和运算符,exp_x相同但有一个或多个出现x???一般来说,你需要做一个数学表达式的解析器,用与Hand类似的方法解析x,我希望它能够解任何方程,其中x等于方程或列表。我想象这个程序所做的是给x分配一个列表,其中的总数是x,解方程,任何一种方程,也许你应该看看numpy模,它应该有工具来解任何类型的多项式方程和方程组以及任何其他函数,因为攻击任何类型的方程都比你所想的更复杂,对其中一些人来说,你可能永远找不到这样的解0=x**2+1
或0=x**2+x+1
没有实数解。另一方面,如果你想做你自己,我可以想出一种方法,但只有当它是一个单项式方程时,我指的是这种a=b*x**c+d
其中a,b,c,d是任意数字。很高兴能帮到你。记住要小心使用eval,如果你不信任你的用户,就不要使用,因为恶意用户可以插入eval执行的东西,这可能会毁了你的一天。。。