I'm making a calculator that can add, subtract, multiply, and divide algebraic terms. I've already made a class that can "make" the algebraic terms, and as of now I want the computer to ask me for an algebraic expression, read it, then register it as one. (I'm really not sure about the correct wording, forgive me, I am new to coding.)
# Make a calculator that adds, subtracts, multiplies, and divides algebraic expressions.
# An algebraic expression has: a coefficient, a variable, and a power
# Class that makes the algebraic expression
class ExpressionMaker:
# Defines the coefficient, variable, and power of the algebraic term.
def __init__(self, coefficient, variable, power):
self.coefficient = coefficient
self.variable = variable
self.power = power
# Types down/returns/defines? the whole algebraic expression as one term.
def term(self):
return "{}{}^{}".format(self.coefficient, self.variable, self.power)
# Examples of algebraic terms of varying coefficients, variables, and powers.
expression_1 = ExpressionMaker(7, "x", 1)
expression_2 = ExpressionMaker(4, "y", 1)
expression_3 = ExpressionMaker(-3, "a", 2)
# Make the program understand what the user inputs and convert it into an algebraic expression.
# Make the program add the algebraic expressions from the user.
# An algebraic term has: a coefficient, a variable, and a power.
# Let the program check if the input has those 3 and if they are in the right order.
expression_holder1 = input("What is your first algebraic expression?: ")
print("You first algebraic expression is: " expression_holder1)
I'm really not sure what to do after this. The most I've been able to think about was to use "If statements" to check if the expression_holders
have an integer(for the coefficient), string(for the variable), and I don't know what to check for the power. I also don't know how to check if the order is correct. For example, the correct input would be 7x^3
but what if they instead type x7^3
. And if the input is wrong, how do I let the user know that and let them type again?
CodePudding user response:
This solution uses regular expression to split the coefficient and variable apart. The power is retrieved using parition. It prints an error if the format of the given input is incorrect.
import re #adds regular expression module
#Code for class can go here
while True:
try:
expression_holder1 = input("What is your first algebraic expression?: ")
expression = expression_holder1.partition("^") #Paritions the expression using the ^, the power if stored in the 2nd index
coefficient_variable = expression[0] #THe coefficient and varible is stored in the first index
res = re.split('(\d )', coefficient_variable) #Uses regular expression to split the character and number
power = expression[2] # Stores the value of given power
coefficient = res[1] # store the value of coefficient
variable = res[2] # Stores the varible given
if power.isdigit() == False or coefficient.isdigit() == False or variable.isalpha() == False:
print("Wrong input") #prints error if the incorrect format is given
else:
break
except IndexError: #prints error if a coefficient, variable or power is missing
print("Index Error -> Wrong input")
expression1 = ExpressionMaker(coefficient, variable, power)
print(expression1.term())