Search code examples
pythonpython-3.xalgebra

python3 algebraic expression solve for x


I'm relatively new to python, and decided to make a calculator. Unfortunately I cant solve for x. The error is:

SyntaxError: can use starred expression only as assignment target.

I can't figure out any way around this as I want to have the person enter in the problem, and then it prints x.

Please help, And thanks for any help in advance.

My code:

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)

Solution

  • here are a few thing about your code:

    the function ints do nothing in the end because you don't return any value, the error that you get come from c=(*z) you can't do that in a assignation, but you can do it in a function call like this fun(*argument_list).

    The variable math as used there is useless because you assign it True and check it for that same value, so you enter in that if block unconditionally meaning that that if math == True is unneeded, maybe you mean while math that way you repeat that block while the variable math is true.

    What is the reason of the variable a_list?, you don't use it.

    In the block if algebra == 'no' you can put int conversions first and then check the operate so to avoid repeat the same code over and over again, speaking of operate the last elif is redundant because if you get there it is because it fail the other comparisons so no need to check again against every possibility, change it for a simple else.

    with that little revisions you code will look like this

    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)
    

    The no algebra part work fine, you need now figure the solve x part, if you need help with this too, just ask :)

    Simple calculator

    After quick search, making a simple calculator in python is easy with the use of eval, like this

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

    with this you can procesate any valid python expression as if you were in the IDLE.

    But if for academics reason you don't want to use it, then as I tell you before, you have to make a parser of mathematical expressions, that identificate what operator is in there and arrange them according to its precedence and finally solve the expresion