samedi 27 juin 2015

python 3.4.3 help: programming calculator with no buttons (entering entire expression)

I am creating a python program and need some help. Essentially I am making a calculator that solves the entire expression (ex. user types in 2 + 2). The program will read the equation the user entered as a string and manipulate the string in order to figure out the operands and the operator of the equation. I cannot use eval() in this script. So here is what I have written so far:

import operator

operList = ["+", "/", "%", "//", "**", "-",""," "]
Error1 = "Something is wrong with your equation: there is nothing in it."
Error2 = "Something is wrong with your equation: it does not feel complete."


def printGreeting():
    print("Hello, welcome to Equation Calculator")
    print("Enter in the expression that you want evaluated and the program will do the rest.")

def getExpression():
    userExp = input("Enter mathematical expression here: ")
    if not userExp.isdigit() or userExp.isalpha():
        print(Error2)
    elif operList[6] or operList[7] in userExp:
        print(Error1)
    else:
        return userExp

def add():
    if operList[0] in userExp:
        userExp.add()




printGreeting()
userExp = getExpression()

As you can see I am stuck on the function call def add(). My question is: Lets say the user entered 2 + 2, how would I go about evaluating the expression? I figured out how the function can recognize the operator + in the userExp but what next?

Note: I am very inexperienced with Python (just started a few weeks ago) and I really want to understand how to do this and any help is appreciated

Aucun commentaire:

Enregistrer un commentaire