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()
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.