如何让程序理解输入
How do I let the program understand the input
我正在制作一个可以加减乘除代数项的计算器。我已经制作了一个可以“制作”代数项的 class,现在我希望计算机向我询问代数表达式,读取它,然后将其注册为一个。 (我真的不确定正确的措辞,请原谅,我是编码新手。)
# 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)
我真的不知道这之后该怎么办。我能想到的最多的是使用“If 语句”来检查 expression_holders
是否有整数(对于系数)、字符串(对于变量),我不知道该怎么做检查电源。我也不知道如何检查顺序是否正确。例如,正确的输入应该是 7x^3
,但是如果他们输入 x7^3
会怎样呢?如果输入错误,我如何让用户知道并让他们重新输入?
此解决方案使用正则表达式将系数和变量分开。使用 parition 检索电源。如果给定输入的格式不正确,它会打印错误。
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())
我正在制作一个可以加减乘除代数项的计算器。我已经制作了一个可以“制作”代数项的 class,现在我希望计算机向我询问代数表达式,读取它,然后将其注册为一个。 (我真的不确定正确的措辞,请原谅,我是编码新手。)
# 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)
我真的不知道这之后该怎么办。我能想到的最多的是使用“If 语句”来检查 expression_holders
是否有整数(对于系数)、字符串(对于变量),我不知道该怎么做检查电源。我也不知道如何检查顺序是否正确。例如,正确的输入应该是 7x^3
,但是如果他们输入 x7^3
会怎样呢?如果输入错误,我如何让用户知道并让他们重新输入?
此解决方案使用正则表达式将系数和变量分开。使用 parition 检索电源。如果给定输入的格式不正确,它会打印错误。
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())