python3代数表达式求解x

时间:2015-12-10 17:08:07

标签: 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(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)

1 个答案:

答案 0 :(得分:0)

以下是您的代码的一些内容:

函数ints最后什么也不做,因为你没有返回任何值,你得到的错误来自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中一样。

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