python3 代数表达式求解 x

python3 algebraic expression solve for x

我对 python 比较陌生,因此决定制作一个计算器。不幸的是我不能解决 x。错误是:

SyntaxError: can use starred expression only as assignment target.

我想不出解决这个问题的办法,因为我想让这个人输入问题,然后打印 x。

请帮忙,并提前感谢您的帮助。

我的代码:

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 最后什么都不做,因为你没有 return 任何值,你得到的错误来自 c=(*z) 你不能在赋值,但你可以在像这样的函数调用中完成 fun(*argument_list)

那里使用的变量 math 是无用的,因为您将其赋值为 True 并检查它是否具有相同的值,因此您无条件地输入该 if 块意味着 if math == True是不需要的,也许你的意思是 while math 当变量 math 为真时你重复那个块。

变量a_list是什么原因,你不用。

在块 if algebra == 'no' 中,您可以先进行 int 转换,然后检查 operate 以避免一遍又一遍地重复相同的代码,谈到 operate 最后一个 elif 是多余的,因为如果你到达那里,那是因为它没有通过其他比较,因此无需再次检查所有可能性,将其更改为简单的 else.

稍作修改后,您的代码将如下所示

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)

没有代数的部分很好用,你现在需要计算求解 x 的部分,如果你也需要这方面的帮助,尽管问:)

简单计算器

经过快速搜索,使用eval在python中制作一个简单的计算器很容易,就像这样

def calculator():
    exp = input("x= ")
    print("x=", eval(exp) )

有了这个,您可以处理任何有效的 python 表达式,就好像您在 IDLE 中一样。

但是如果出于学术原因你不想使用它,那么就像我之前告诉你的那样,你必须制作一个数学表达式的解析器,它可以识别其中的运算符并根据其优先级排列它们最后求解表达式