简体   繁体   English

如何让程序理解输入

[英]How do I let the program understand the input

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.我已经制作了一个可以“制作”代数项的 class,现在我希望计算机向我询问代数表达式,读取它,然后将其注册为一个。 (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.我能想到的最多的是使用“If statements”来检查expression_holders是否有整数(对于系数),字符串(对于变量),我不知道要检查什么幂. 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 .例如,正确的输入应该是7x^3但如果他们改为输入x7^3会怎样。 And if the input is wrong, how do I let the user know that and let them type again?如果输入错误,我如何让用户知道并让他们再次输入?

This solution uses regular expression to split the coefficient and variable apart.该解决方案使用正则表达式将系数和变量分开。 The power is retrieved using parition.使用 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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM