-1

so I'm kind of new to programming and I was looking for some help. I'm making a graphing calculator and would like to have the user enter an equation using x such as (x + 3) or (x^2 + 3x + 4). I recently found out about the lambda function and was wondering if there was a way to pass a variable to it in order to get plot points with the user's equation. I plan on using a for loop to keep passing new values into the equation. If you have any other suggestions on how to go about completing my graphing calculator please do not hesitate to inform me. My code so far is only a way for the user to navigate through my program. Here is my code:

def main():
    while True:
        response = menu()
        if response == "1":
            print("enter an equation in the form of mx + b")
            equation = (input())
            print(coordinates(equation))
        elif response == "2":
            print("enter a value for x")
            x = input()
            print("enter a value for y")
            y = input()
        elif response == "0":
            print("Goodbye")
            break
        else:
            print("please enter '1' '2' or '0'")

def menu():
    print("Graphing Calculator")
    print("0) Quit")
    print("1) Enter an equation")
    print("2) Plot points")
    print("Please select an option")
    response = input()
    return response

"""def coordinates(equation):
    f = lambda x: equation
"""

if __name__ == "__main__":
    main()
Matteo
  • 35
  • 10

2 Answers2

2

I get that I'm answering your question twice. But this time, I'm gonna show you something pretty freaking cool instead of just fixing your code.

Enter: Sympy. Sympy is a module of the Numpy package that does symbolic manipulation. You can find some documentation here.

Great, another module. But what does it do?

Sympy keeps equations and expressions as symbols instead of variables. Go ahead and import sympy or pip install it if you don't have it. Now, let's build an evaluation unit for that monovariate equation coordinate finder. First, let's make sure python knows that x is a sympy symbol. This is important because otherwise, python treats it like a normal variable and doesn't hand control over to sympy. Sympy makes it easy to do this, and even supports making multiple symbols at once. So go ahead and do x, y, z = sympy.symbols("x y z"). Now, let's ask for the function.

def make_an_equation(raw_equation = ""):
  if raw_equation == "": #This allows for make_an_equation to be used from the console AND as an interface. Spiffy, no?
    raw_equation = raw_input("Y=")
  #filter and rewrite the equation for python compatibility. Ex. if your raw_equation uses ^ for exponentiation and XOR for exclusive or, you'd turn them into ** and ^, respectively. 
  expr = eval(filtered_equation)
  return expr

Ok, you've got a thing that can make an equation. But... You can't quite use it, yet. It's entirely symbolic. You'll have to use expr.subs to substitute numbers for x.

>>> my_expression = make_an_equation("x**2") #I'm assuming that the filter thing was never written. 
>>> my_expression
x**2
>>> my_expression.subs(x,5)
25

See how easy that is? And you can get even more complex with it, too. Let's substitute another symbol instead of a number.

>>> my_expression.subs(x,z)
z**2

Sympy includes a bunch of really cool tools, like trig functions and calculus stuff. Anyways, there you go, intro to Sympy! Have fun!

Jakob Lovern
  • 1,301
  • 7
  • 24
1

So... I went ahead and reviewed your whole code. I'll walk you through what I did in comments.

def main():
    while True:
        response = raw_input("Graphing Calculator\n0) Quit\n1) Enter an equation\n2) Plot points\nPlease select an option\n>>>")

        #I removed menu() and condensed the menu into a single string. 
        #Remember to use raw_input if you just want a string of what was input; using input() is a major security risk because it immediately evaluates what was done. 

        if response == "1":
            print("Enter an equation")
            equation = raw_input("Y=")  #Same deal with raw_input. See how you can put a prompt in there? Cool, right?
            finished_equation = translate_the_equation(equation)
            #This is to fix abnormalities between Python and math. Writing this will be left as an exercise for the reader.
            f = coordinates(finished_equation)
            for i in xrange(min_value,max_value): 
              print((i,f(i))) #Prints it in (x,y) coordinate pairs.
        elif response == "2":
            x = raw_input("X=? ")
            y = raw_input("Y=? ")
            do_something(x,y)
        elif response == "0":
            print("Goodbye")
            break
        else:
            print("please enter '0', '1', or '2'") #I ordered those because it's good UX design. 
coordinates = lambda equation: eval("lambda x:"+ equation)
#So here's what you really want. I made a lambda function that makes a lambda function. eval() takes a string and evaluates it as python code. I'm concatenating the lambda header (lambda x:) with the equation and eval'ing it into a true lambda function.
Jakob Lovern
  • 1,301
  • 7
  • 24
  • It's a good answer but it should be noted that there are differences between usual mathematical syntax and Python expressions. For instance, `mx+b` in Python is the sum of the `mx` object and `b` object; whereas in math it would probably be understood as `m*x + b`. If OP really wants to get `mx+b` as in math, some form of parsing/translation will be needed. – Francis Colas Nov 27 '17 at 22:05
  • @FrancisColas yeah, but that's off topic and, frankly, I hate dealing with parsers. I'll add a section handwaving parsing in. – Jakob Lovern Nov 27 '17 at 22:08